Я новичок во Флаттере и застрял с чем-то тривиальным, чего я не могу понять.
Вот класс с состоянием:
class Menu extends StatefulWidget {
Menu({Key key}) : super(key: key);
@override
MenuState createState() => MenuState();
}
class MenuState extends State<Menu> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Padding(padding: EdgeInsets.fromLTRB(0.0, 50.0, 0.0, 0.0)),
new FetchSummary(context: context),
],
),
);
}
}
Из этого класса я звоню FetchSummary. Идея состоит в том, чтобы извлечь данные из файла JSON, который будет динамически изменяться позже.
class FetchSummary extends StatefulWidget {
FetchSummary({Key key, this.context}) : super(key: key);
final BuildContext context;
@override
FetchSummaryState createState() => FetchSummaryState();
}
class FetchSummaryState extends State<FetchSummary> {
var _jsonFile = "assets/db-summaryData.json";
@override
Widget build(BuildContext context) {
return new FutureBuilder(
future: DefaultAssetBundle.of(context).loadString(_jsonFile),
builder: (context, snapshot) {
var myData = json.decode(snapshot.data.toString());
if (snapshot.data == null) {
return Container(child: Center(child: Text("Loading...")));
} else {
//List<Post> yourPosts = snapshot.data.posts;
return Column(children: <Widget>[
Flexible(
child: ListView.builder(
//scrollDirection: Axis.vertical,
//shrinkWrap: true,
itemCount: myData.length,
padding: const EdgeInsets.only(bottom: 50),
itemBuilder: (BuildContext context, int index) {
return Container(
//margin: const EdgeInsets.symmetric(vertical: 1),
color: Color.fromRGBO(107, 164, 147, 0.5),
child: ListTile(
title: new Text(
"Dummy Text",
),
));
}))
]);
}
});
}
}
В этом виджете в данный момент я просто пытаюсь отправить обратно некоторый текст, прежде чем что-то извлечь из myData JSON объект. Однако, когда я пытаюсь запустить его, он начинает жаловаться, как показано ниже:
The following assertion was thrown during performResize():
**Vertical viewport was given unbounded width.**
Viewports expand in the cross axis to fill their container and constrain their children to match their extent in the cross axis. In this case, a vertical viewport was given an unlimited amount of horizontal space in which to expand.
The relevant error-causing widget was:
ListView file:///D:/FlutterDev/Dashboard/dashboard/lib/main.dart:149:35
When the exception was thrown, this was the stack:
#0 RenderViewport.performResize.<anonymous closure> (package:flutter/src/rendering/viewport.dart:1191:15)
Обновляется
Если я перемещаю / копирую FutureBuilder в вызывающую функцию вместо вызова этого класса, он работает нормально. **
Widget build(BuildContext context) {
return new FutureBuilder(
future: DefaultAssetBundle.of(context).loadString(_jsonFile),
builder: (context, snapshot) {
var myData = json.decode(snapshot.data.toString());
if (snapshot.data == null) {
return Container(child: Center(child: Text("Loading...")));
..............
Окончательное обновление
Я заменил код вызова с этого:
return new Container(
child: new Row(
children: <Widget>[
new Padding(padding: EdgeInsets.fromLTRB(0.0, 50.0, 0.0, 0.0)),
new FetchSummary(context: context),
],
),
);
на это:
return new Container(
child:
new FetchSummary(context: context),
);
И это работал. Я думаю, что ему не понравился виджет Row.