Как выполнить foreach в языке Dart (Flutter)?
Данные ввел из json-а , все работает отлично , но сейчас выводиться только первый элемент. Как мне написать здесь типо foreach-а чтобы все элементы с циклом вывелись на экран. Строго не судите, я новичок. Спасибо.
class TreePage extends StatefulWidget {
String vp;
TreePage({this.vp});
@override
_TreePageState createState() => _TreePageState();
}
class _TreePageState extends State<TreePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text('Текст',
style: TextStyle(color: Colors.white,),
),
centerTitle: true,
),
body: new FutureBuilder(
future: getPhoneFinal(widget.vp),
//future: codePhone(widget.code),
builder: (context, snapshot){
List data = snapshot.data;
if(snapshot.hasError){
print (snapshot.error);
return Text('Не удалось получить ответ от сервера',
style: TextStyle(color: Colors.red,
fontSize: 22.0)
);
}
else if(snapshot.hasData ){
return new Container(
child:Column(
children: <Widget>[
new Padding(padding: const EdgeInsets.all(5.0)),
new Container(
child: new Text(
'Тип штрафа: ${data[0]['VDescription']}'
),
),
new Container(
child: new Text(
'Адрес: ${data[0]['VLocation']}'
),
),
new Container(
child: new Text(
'Дата: ${data[0]['VTime']}'
),
),
],
)
);
}else if(!snapshot.hasData){
return new Center(child: CircularProgressIndicator(),);
}
}),
);
}
getFine(String vp) {}
}
Future<List> getPhoneFinal(String numbCar) async{
String url = 'http://xxx/xxx?xxx=$numbCar';
http.Response response = await http.get(url);
return json.decode(utf8.decode(response.bodyBytes));
}
Ответы (1 шт):
Автор решения: MiT
→ Ссылка
1 вариант (Его следует использовать для маленьких списков):
class _TreePageState extends State<TreePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text('Текст',
style: TextStyle(color: Colors.white,),
),
centerTitle: true,
),
body: new FutureBuilder(
future: getPhoneFinal(widget.vp),
//future: codePhone(widget.code),
builder: (context, snapshot){
if(snapshot.hasError){
print (snapshot.error);
return Text('Не удалось получить ответ от сервера',
style: TextStyle(color: Colors.red,
fontSize: 22.0)
);
}
else if(snapshot.hasData && snapshot.data.isNotEmpty){
List data = snapshot.data;
for(int i =0; i< data.length; i++) {
return new Container(
child:Column(
children: <Widget>[
new Padding(padding: const EdgeInsets.all(5.0)),
new Container(
child: new Text(
'Тип штрафа: ${data[0]['VDescription']}'
),
),
new Container(
child: new Text(
'Адрес: ${data[0]['VLocation']}'
),
),
new Container(
child: new Text(
'Дата: ${data[0]['VTime']}'
),
),
],
)
);
}
} else{
return new Center(child: CircularProgressIndicator(),);
}
}),
);
}
getFine(String vp) {}
}
2 вариант (Его следует использовать для больших списков):
class _TreePageState extends State<TreePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text('Текст',
style: TextStyle(color: Colors.white,),
),
centerTitle: true,
),
body: new FutureBuilder(
future: getPhoneFinal(widget.vp),
//future: codePhone(widget.code),
builder: (context, snapshot){
if(snapshot.hasError){
print (snapshot.error);
return Text('Не удалось получить ответ от сервера',
style: TextStyle(color: Colors.red,
fontSize: 22.0)
);
}
else if(snapshot.hasData && snapshot.data.isNotEmpty){
List data = snapshot.data;
return new Container(
child: ListView.builder(
itemCount: data.length,
itemBuilder: (BuildContext context, int index) {
return Column(
children: <Widget>[
new Padding(padding: const EdgeInsets.all(5.0)),
new Container(
child: new Text(
'Тип штрафа: ${data[0]['VDescription']}'
),
),
new Container(
child: new Text(
'Адрес: ${data[0]['VLocation']}'
),
),
new Container(
child: new Text(
'Дата: ${data[0]['VTime']}'
),
),
],
);},),
);
}else{
return new Center(child: CircularProgressIndicator(),);
}
}),
);
}
getFine(String vp) {}
}