英文:
How do I add ListView elements using FloatingActionButton in flutter?
问题
I'm currently learning flutter and I'm having trouble managing states. I used this flutter guide as a reference: https://docs.flutter.dev/ui/interactive#managing-state.
I'm getting an exception thrown: "Lookup failed: addTiles in @methods in _MyTileState in package:app_recipes/main.dart"
This is my code:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHome(),
);
}
}
class MyHome extends StatelessWidget {
const MyHome({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: const Text('My App'),
),
body: const MyTile(),
);
}
}
class MyTile extends StatefulWidget {
const MyTile({super.key});
@override
State<MyTile> createState() => _MyTileState();
}
class _MyTileState extends State<MyTile> {
int _tileCount = 5;
void _addTiles() {
setState(() {
_tileCount += 1;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: _MyTileState()._tileCount,
itemBuilder: (BuildContext context, index) {
return ListTile(
leading: const Icon(Icons.list),
trailing: Text(
'Tile $_tileCount',
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: _addTiles,
child: const Icon(Icons.add),
),
);
}
}
英文:
I'm currently learning flutter and I'm having trouble managing states. I used this flutter guide as a reference: https://docs.flutter.dev/ui/interactive#managing-state.
I'm getting an exception thrown: "Lookup failed: addTiles in @methods in _MyTileState in package:app_recipes/main.dart"
This is my code:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHome(),
);
}
}
class MyHome extends StatelessWidget {
const MyHome({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: const Text('My App'),
),
body: const MyTile(),
);
}
}
class MyTile extends StatefulWidget {
const MyTile({super.key});
@override
State<MyTile> createState() => _MyTileState();
}
class _MyTileState extends State<MyTile> {
int _tileCount = 5;
void _addTiles()
{
setState(() {
_tileCount += 1;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: _MyTileState()._tileCount,
itemBuilder: (BuildContext context, index)
{
return ListTile(
leading: const Icon(Icons.list),
trailing: Text(
'Tile $_tileCount'
),
);
}
),
floatingActionButton: FloatingActionButton(
onPressed: _addTiles,
child: const Icon(Icons.add),
),
);
}
}
答案1
得分: 0
我解决了,尽管我还没有完全理解为什么这会导致问题。
你的列表项的尾部属性显示的不是瓷砖的当前索引,而是瓷砖数量的计数。这将为每个瓷砖显示相同的字符串,但不应该在添加更多瓷砖时出现问题。尽管如此,将 Tile $_tileCount
更改为 Tile $index
以解决问题。
英文:
I solved it even tough I haven't fully understood why this causes the problem.
The trailing property of your list tiles displays not the current index of the tile but rather the count of the amount of tiles. This will show the same string for every tile but should not make a problem adding more tiles. Nevertheless, change Tile $_tileCount
to Tile $index
to fix it.
return ListTile(
leading: const Icon(Icons.list),
trailing: Text(
'Tile $index'
),
);
答案2
得分: 0
你正在通过_MyTileState()
访问_tileCount
,这是一个通用对象,而不是当前实例。
你可以使用this._tileCount
或者直接使用_tileCount
。
英文:
You are accessing _tileCount
via _MyTileState()
, which is the Generic Object, not the current instance.
You could use this._tileCount
or just outright _tileCount
.
答案3
得分: 0
请使用 _tileCount 而不是 _MyTileState()._tileCount,因为您通过调用类的构造函数创建了新实例,所以它会恢复到默认值。
如果您想显式指向这个类,应该使用 this._tileCount,它引用当前类。
英文:
You should use _tileCount instead of _MyTileState()._tileCount because u are creating new instace by calling constructor of class so it reverts to default.
if you want to explicitly point for this class u should use this._tileCount it refers to current class.
答案4
得分: 0
Every Think fine but the way you accessed the _tileCount is not valid
To access the _tileCount variable within the same class, you can directly refer to it by its name without mentioning the class, as they are both in the same scope.
so I just replaced
itemCount: _MyTileState()._tileCount,
to
itemCount: _tileCount,
or
itemCount: this._tileCount,
Here is the final Code:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHome(),
);
}
}
class MyHome extends StatelessWidget {
const MyHome({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: const Text('My App'),
),
body: const MyTile(),
);
}
}
class MyTile extends StatefulWidget {
const MyTile({Key? key});
@override
State<MyTile> createState() => _MyTileState();
}
class _MyTileState extends State<MyTile> {
int _tileCount = 5;
void _addTiles() {
setState(() {
_tileCount += 1;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: _tileCount,
itemBuilder: (BuildContext context, index) {
return ListTile(
leading: const Icon(Icons.list),
trailing: Text('Tile $index'),
);
}),
floatingActionButton: FloatingActionButton(
onPressed: _addTiles,
child: const Icon(Icons.add),
),
);
}
}
英文:
Every Think fine but the way you accessed the _tileCount is not valid
To access the _tileCount variable within the same class, you can directly refer to it by its name without mentioning the class, as they are both in the same scope.
so I just replaced
itemCount: _MyTileState()._tileCount,
to
itemCount: _tileCount,
or
itemCount: this._tileCount,
Here is the final Code :
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHome(),
);
}
}
class MyHome extends StatelessWidget {
const MyHome({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: const Text('My App'),
),
body: const MyTile(),
);
}
}
class MyTile extends StatefulWidget {
const MyTile({super.key});
@override
State<MyTile> createState() => _MyTileState();
}
class _MyTileState extends State<MyTile> {
int _tileCount = 5;
void _addTiles()
{
setState(() {
_tileCount += 1;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: _tileCount,
itemBuilder: (BuildContext context, index)
{
return ListTile(
leading: const Icon(Icons.list),
trailing: Text(
'Tile $index'
),
);
}
),
floatingActionButton: FloatingActionButton(
onPressed: _addTiles,
child: const Icon(Icons.add),
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论