Flutter在模拟器中捕获的异常错误和红色的堆栈溢出。

huangapple go评论112阅读模式
英文:

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 &#39;package:flutter/material.dart&#39;;
void main() {
runApp(const TodoApp());
}
class TodoApp extends StatelessWidget {
const TodoApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: &#39;Todo Uygulaması&#39;,
theme: ThemeData(
primarySwatch: Colors.pink,
),
home: TodoApp()
//TodoList(title: &#39;Todo Uygulaması&#39;),
);
}
}
class TodoList extends StatefulWidget {
TodoList({super.key, required this.title});
final String title;
@override
State&lt;TodoList&gt; createState() =&gt; _TodoListState();
}
enum moreVert { rechange, delete }
TextEditingController t1 = TextEditingController();
List TodoVerileri = [];
class _TodoListState extends State&lt;TodoList&gt; {
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(&#39;Todo&#39;),
),
)),
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: &lt;Widget&gt;[Tasks()],
),
),
floatingActionButton: _FloatingActionButton(context),
);
}
Flexible Tasks() {
return Flexible(
child: ListView.builder(
itemBuilder: (context, indexnum) =&gt; Row(
children: [
ListTile(title: Text(TodoVerileri[indexnum])),
],
),
itemCount: TodoVerileri.length,
));
}
FloatingActionButton _FloatingActionButton(BuildContext context) {
return FloatingActionButton(
onPressed: () {
pressFun(context);
},
tooltip: &#39;Increment&#39;,
child: const Icon(Icons.add),
);
}
Future&lt;dynamic&gt; pressFun(BuildContext context) {
return showDialog(
context: context,
builder: (ctx) =&gt; AlertDialog(
title: const Text(&quot;Type Your Task Here&quot;),
actions: [
TextField(
controller: t1,
),
Center(
child: TextButton(
onPressed: () {
Navigator.pop(context);
veriEkle();
},
child: Text(&#39;Create&#39;))),
Center(
child: TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text(&#39;Cancel&#39;)))
],
));
}
}
class PopUpMen extends StatefulWidget {
const PopUpMen({super.key});
@override
State&lt;PopUpMen&gt; createState() =&gt; _PopUpMenState();
}
class _PopUpMenState extends State&lt;PopUpMen&gt; {
@override
Widget build(BuildContext context) {
return PopupMenuButton(
onSelected: (value) {
// your logic
},
itemBuilder: (BuildContext bc) {
return const [
PopupMenuItem(
child: Row(
children: [Icon(Icons.edit), Text(&#39;Edit&#39;)],
)),
PopupMenuItem(
child: Row(
children: [Icon(Icons.delete), Text(&#39;Delete&#39;)],
)),
];
},
);
}
}

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.

(https://i.stack.imgur.com/o14Wl.png)

答案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) =&gt; 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) =&gt; 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) =&gt; Row(
children: [
SizedBox(
width: MediaQuery.of(context).size.width,
child: ListTile(title: Text(TodoVerileri[indexnum]))),
],
),
itemCount: TodoVerileri.length,
),
);
}

huangapple
  • 本文由 发表于 2023年7月11日 00:16:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定