我能够创建一个SQFLite数据库并运行查询,但是,当我尝试使用listview.separated小部件显示结果时,我收到了noSuchMethodError。复习了其他类似的堆栈溢出主题,但没有运气。代码错误-创建局部变量?
void main() => runApp(MyApp());
final dbHelper = DatabaseHelper();
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
List<Breeds> allBreedsList;
return MaterialApp(
title: 'My 1st App',
home: Scaffold(
appBar: AppBar(
title: Text('Pets, Pets, Pets'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text(
'query',
style: TextStyle(fontSize: 20),
),
onPressed: () {
print("Yeah, it works!");
{
_pullAllbreed();
}
Expanded(
child: ListView.separated(
itemBuilder: (context, index) {
return ListTile(
leading: Text("${allBreedsList[index]}"),
title: Text("${allBreedsList[index].name}"),
);
},
separatorBuilder: (context, index) => Divider(),
itemCount: allBreedsList.length,
));
}),
]))));
}
}
void _pullAllbreed() async {
final allBreedsList = await dbHelper.breedList();
print('querying all breeds now, please stand by');
allBreedsList.forEach((row) => print(row));
int breedCount = allBreedsList.length;
print(breedCount);
}
答案 0 :(得分:0)
您可以在下面复制粘贴运行完整代码
步骤1:从ListView.separated
中移出RaisedButton
步骤2:从List<Breeds> allBreedsList
中移出build
步骤3:MyApp
使用StatefulWidget
,并在setState(() {});
之后致电_pullAllbreed();
onPressed: () {
print("Yeah, it works!");
_pullAllbreed();
setState(() {});
}),
工作演示
完整代码
import 'package:flutter/material.dart';
class DatabaseHelper {
Future<List<Breeds>> breedList() {
return Future.value(
[Breeds(name: "a"), Breeds(name: "b"), Breeds(name: "c")]);
}
}
class Breeds {
String name;
Breeds({this.name});
}
void main() => runApp(MyApp());
final dbHelper = DatabaseHelper();
List<Breeds> allBreedsList = [];
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My 1st App',
home: Scaffold(
appBar: AppBar(
title: Text('Pets, Pets, Pets'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text(
'query',
style: TextStyle(fontSize: 20),
),
onPressed: () {
print("Yeah, it works!");
_pullAllbreed();
setState(() {});
}),
Expanded(
child: ListView.separated(
itemBuilder: (context, index) {
return ListTile(
leading: Text("${allBreedsList[index]}"),
title: Text("${allBreedsList[index].name}"),
);
},
separatorBuilder: (context, index) => Divider(),
itemCount: allBreedsList.length,
))
]))));
}
}
void _pullAllbreed() async {
allBreedsList = await dbHelper.breedList();
print('querying all breeds now, please stand by');
allBreedsList.forEach((row) => print(row));
int breedCount = allBreedsList.length;
print(breedCount);
}