我有一个StatefulWidget
用于显示包含数据的页面,并且我正在使用setState
返回该页面,但是当我运行应用程序时显示如下错误:
[1]:release note“截屏”
这是我的代码
import 'package:flutter/material.dart';
import 'package:codelab_dicoding/data.dart';
import 'package:codelab_dicoding/detail.dart';
class BreakFast extends StatefulWidget{
@override
_BreakFastState createState() => _BreakFastState();
}
class _BreakFastState extends State<BreakFast>{
List<Container> daftarBreakfast = List();
@override
Widget build(BuildContext context) {
// TODO: implement build
Future <_BreakFastState>_breakfast() async {
for (var i = 0; i < breakfast.length; i++) {
final namaMakanan = breakfast[i];
final String gambar = namaMakanan["gambar"];
final String bahan = namaMakanan["bahan"];
daftarBreakfast.add(Container(
padding: EdgeInsets.all(10.0),
child: Card(
child: Column(
children: <Widget>[
Expanded(
// tag: namaMakanan['nama'],
child: Material(
child: InkWell(
onTap: () =>
Navigator.of(context).push(new MaterialPageRoute(
builder: (BuildContext context) => Detail(
nama: namaMakanan['nama'],
gambar: gambar,
bahan: bahan
),
)),
child: Image.network(
gambar,
fit: BoxFit.cover,
),
),
),
),
Padding(
padding: EdgeInsets.all(10.0),
),
Text(
namaMakanan['nama'],
style: new TextStyle(fontSize: 20.0),
)
],
))));
}
}
setState(() {
return BreakFast();
});
}
}
我的代码有问题吗?为了简单起见,我只是省略了一些变量。
答案 0 :(得分:0)
您犯了一些错误,例如您在setState
内使用build()
,并且在_breakfast()
内也有build()
。签出此代码,它正在工作。
class BreakFast extends StatefulWidget {
@override
_BreakFastState createState() => _BreakFastState();
}
class _BreakFastState extends State<BreakFast> {
List<Container> daftarBreakfast = List();
// put it outside build()
Future<_BreakFastState> _breakfast() async {
for (var i = 0; i < breakfast.length; i++) {
final namaMakanan = breakfast[i];
final String gambar = namaMakanan["gambar"];
final String bahan = namaMakanan["bahan"];
daftarBreakfast.add(Container(
padding: EdgeInsets.all(10.0),
child: Card(
child: Column(
children: <Widget>[
Expanded(
// tag: namaMakanan['nama'],
child: Material(
child: InkWell(
onTap: () => Navigator.of(context).push(new MaterialPageRoute(
builder: (BuildContext context) => Detail(nama: namaMakanan['nama'], gambar: gambar, bahan: bahan),
)),
child: Image.network(
gambar,
fit: BoxFit.cover,
),
),
),
),
Padding(
padding: EdgeInsets.all(10.0),
),
Text(
namaMakanan['nama'],
style: new TextStyle(fontSize: 20.0),
)
],
))));
}
}
@override
Widget build(BuildContext context) {
// return BreakFast();
return FlutterLogo(size: 400); // try this
}
}