英文:
Flutter Exception caught by widgets library error and Stack Overflow with red color in simulator
问题
我在stackoverflow上是新手,请提醒我如果我的代码中有缺失的信息。
我编写了一些代码,但在模拟器中,出现了我添加的照片中的错误。
我的代码:
import 'package:flutter/material.dart';
void main() {
runApp(const TodoApp());
}
class TodoApp extends StatelessWidget {
const TodoApp({Key? key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Todo Uygulaması',
theme: ThemeData(
primarySwatch: Colors.pink,
),
home: TodoList(title: 'Todo Uygulaması'),
);
}
}
class TodoList extends StatefulWidget {
TodoList({Key? key, required this.title});
final String title;
@override
State<TodoList> createState() => _TodoListState();
}
TextEditingController t1 = TextEditingController();
List TodoVerileri = [];
class _TodoListState extends State<TodoList> {
void veriEkle() {
setState(() {
TodoVerileri.add(t1.text);
t1.clear();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: Drawer(
child: ListTile(
onTap: () {},
title: Padding(
padding: EdgeInsets.only(top: 100),
child: Text('Todo'),
),
),
),
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: <Widget>[Tasks()],
),
),
floatingActionButton: _FloatingActionButton(context),
);
}
Flexible Tasks() {
return Flexible(
child: ListView.builder(
itemBuilder: (context, indexnum) => Row(
children: [
ListTile(title: Text(TodoVerileri[indexnum])),
],
),
itemCount: TodoVerileri.length,
),
);
}
FloatingActionButton _FloatingActionButton(BuildContext context) {
return FloatingActionButton(
onPressed: () {
pressFun(context);
},
tooltip: 'Increment',
child: const Icon(Icons.add),
);
}
Future<dynamic> pressFun(BuildContext context) {
return showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: const Text("Type Your Task Here"),
actions: [
TextField(
controller: t1,
),
Center(
child: TextButton(
onPressed: () {
Navigator.pop(context);
veriEkle();
},
child: Text('Create'),
),
),
Center(
child: TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Cancel'),
),
),
],
),
);
}
}
class PopUpMen extends StatefulWidget {
const PopUpMen({Key? key});
@override
State<PopUpMen> createState() => _PopUpMenState();
}
class _PopUpMenState extends State<PopUpMen> {
@override
Widget build(BuildContext context) {
return PopupMenuButton(
onSelected: (value) {
// your logic
},
itemBuilder: (BuildContext bc) {
return const [
PopupMenuItem(
child: Row(
children: [Icon(Icons.edit), Text('Edit')],
),
),
PopupMenuItem(
child: Row(
children: [Icon(Icons.delete), Text('Delete')],
),
),
];
},
);
}
}
错误代码:
在widgets库中发生的异常 ═══════════════════════════════════
'package:flutter/src/widgets/framework.dart': 断言失败:第 4809 行位置 16:'owner!._debugCurrentBuildTarget == this': 不是真实的。
(https://i.stack.imgur.com/o14Wl.png)
英文:
I'm new at stackoverflow, please warn me If I have missing information in my code.
I wrote some codes but in simulator, it giving error as in photo I added.
My code:
import 'package:flutter/material.dart';
void main() {
runApp(const TodoApp());
}
class TodoApp extends StatelessWidget {
const TodoApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Todo Uygulaması',
theme: ThemeData(
primarySwatch: Colors.pink,
),
home: TodoApp()
//TodoList(title: 'Todo Uygulaması'),
);
}
}
class TodoList extends StatefulWidget {
TodoList({super.key, required this.title});
final String title;
@override
State<TodoList> createState() => _TodoListState();
}
enum moreVert { rechange, delete }
TextEditingController t1 = TextEditingController();
List TodoVerileri = [];
class _TodoListState extends State<TodoList> {
void veriEkle() {
setState(() {
TodoVerileri.add(t1.text);
t1.clear();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: Drawer(
child: ListTile(
onTap: () {},
title: Padding(
padding: EdgeInsets.only(top: 100),
child: Text('Todo'),
),
)),
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: <Widget>[Tasks()],
),
),
floatingActionButton: _FloatingActionButton(context),
);
}
Flexible Tasks() {
return Flexible(
child: ListView.builder(
itemBuilder: (context, indexnum) => Row(
children: [
ListTile(title: Text(TodoVerileri[indexnum])),
],
),
itemCount: TodoVerileri.length,
));
}
FloatingActionButton _FloatingActionButton(BuildContext context) {
return FloatingActionButton(
onPressed: () {
pressFun(context);
},
tooltip: 'Increment',
child: const Icon(Icons.add),
);
}
Future<dynamic> pressFun(BuildContext context) {
return showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: const Text("Type Your Task Here"),
actions: [
TextField(
controller: t1,
),
Center(
child: TextButton(
onPressed: () {
Navigator.pop(context);
veriEkle();
},
child: Text('Create'))),
Center(
child: TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Cancel')))
],
));
}
}
class PopUpMen extends StatefulWidget {
const PopUpMen({super.key});
@override
State<PopUpMen> createState() => _PopUpMenState();
}
class _PopUpMenState extends State<PopUpMen> {
@override
Widget build(BuildContext context) {
return PopupMenuButton(
onSelected: (value) {
// your logic
},
itemBuilder: (BuildContext bc) {
return const [
PopupMenuItem(
child: Row(
children: [Icon(Icons.edit), Text('Edit')],
)),
PopupMenuItem(
child: Row(
children: [Icon(Icons.delete), Text('Delete')],
)),
];
},
);
}
}
Error code:
Exception caught by widgets library ═══════════════════════════════════
'package:flutter/src/widgets/framework.dart': Failed assertion: line 4809 pos 16: 'owner!._debugCurrentBuildTarget == this': is not true.
答案1
得分: 0
首选解决方案
移除 Row 小部件,仅使用 ListTile:
Flexible Tasks() {
return Flexible(
child: ListView.builder(
itemBuilder: (context, indexnum) => ListTile(title: Text(TodoVerileri[indexnum])),
itemCount: TodoVerileri.length,
),
);
}
替代解决方案1
需要将 Tasks 小部件更新为以下内容(将 ListTile 包装在 Expanded 中):
Flexible Tasks() {
return Flexible(
child: ListView.builder(
itemBuilder: (context, indexnum) => Row(
children: [
Expanded(child: ListTile(title: Text(TodoVerileri[indexnum]))),
],
),
itemCount: TodoVerileri.length,
));
}
当您直接在 Row 内部使用 ListTile 时,它无法确定它可以有多宽。
替代解决方案2
将 ListTile 包装在 SizedBox 中,并明确设置宽度为屏幕的宽度:
Flexible Tasks() {
return Flexible(
child: ListView.builder(
itemBuilder: (context, indexnum) => Row(
children: [
SizedBox(
width: MediaQuery.of(context).size.width,
child: ListTile(title: Text(TodoVerileri[indexnum]))),
],
),
itemCount: TodoVerileri.length,
),
);
}
英文:
Preferred Solution
Remove the Row widget and just use the ListTile:
Flexible Tasks() {
return Flexible(
child: ListView.builder(
itemBuilder: (context, indexnum) => ListTile(title: Text(TodoVerileri[indexnum])),
itemCount: TodoVerileri.length,
),
);
}
Alternative Solution 1
You need to update your Tasks widget to this (Wrap the ListTile in an Expanded):
Flexible Tasks() {
return Flexible(
child: ListView.builder(
itemBuilder: (context, indexnum) => Row(
children: [
Expanded(child: ListTile(title: Text(TodoVerileri[indexnum]))),
],
),
itemCount: TodoVerileri.length,
));
}
When you have a ListTile directly inside of a Row, it doesn't know exactly how wide it can be.
Alternative Solution 2
Wrap your ListTile in a SizedBox and explicitly set the width to the width of the screen:
Flexible Tasks() {
return Flexible(
child: ListView.builder(
itemBuilder: (context, indexnum) => Row(
children: [
SizedBox(
width: MediaQuery.of(context).size.width,
child: ListTile(title: Text(TodoVerileri[indexnum]))),
],
),
itemCount: TodoVerileri.length,
),
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论