简体   繁体   English

问题在futureBuilder颤抖中创建listView

[英]Problem create listView in futureBuilder flutter

I want to create a listView in FutureBuilder. 我想在FutureBuilder中创建一个listView。 It's a List of Json Object, when i call the api, i receive the multiples objects but when i want created the list, I have differents errors. 这是一个Json对象列表,当我调用api时,我收到了倍数对象,但是当我要创建列表时,我遇到了不同的错误。

For exemples : 例如:

A build function returned null. 生成函数返回null。 User-created ancestor of the error-causing widget was Expanded 用户创建的导致错误的小部件的祖先已展开

Image : bug 图片: 错误

Theme model: 主题模型:

class Theme {

  int id;
  String name;

  Theme({this.id, this.name});

  factory Theme.fromJson(Map<String, dynamic> json) {
    return Theme(
      id: json['id'],
      name: json['name'],
    );
  }

  Future<List<Theme>> getThemes() async {
  String url = 'http://10.0.2.2:3000/v1/api/theme';
  final response = await http.get(url, headers: {"Accept": "application/json"});

  if (response.statusCode == 200) {
    List themesList = jsonDecode(response.body);
    List<Theme> themes = [];
    for(var themeMap in themesList){
      themes.add(Theme.fromJson(themeMap));
    }
    return themes;
  } else {
    throw Exception('Failed to load themes');
  }
}

}

Theme page : 主题页面:

class _Theme extends State<Theme> {
  var name;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Blackbox"),
        ),
        body: new Center(
          child : new Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              new Container(
                margin: EdgeInsets.only(top: 10.0, bottom: 10.0),
                child: new Text('Sélection du profil',
                    style: new TextStyle(
                        fontSize: 24, fontWeight: FontWeight.bold)),
                        alignment: Alignment.topCenter),
              new Expanded(
                child : new FutureBuilder<List<t.Theme>>(
                    future: t.Theme().getThemes(),
                    builder: (context, snapshot){
                      if (!snapshot.hasData){
                        return CircularProgressIndicator();
                      }else if(snapshot.hasError){
                        return Text("${snapshot.error}");
                      }else{
                        new ListView.builder(
                          itemCount: snapshot.data.length,
                          itemBuilder: (BuildContext context, index) {
                            for (var s in snapshot.data){
                              name = s.name;
                            }
                            return new ListTile(
                              title: new Text(name),
                            );
                          },
                        );
                      }                   
                    },
              )
              ),
              new Container(
              margin: EdgeInsets.only(right: 10.0),
              child: new RaisedButton.icon(
                /*onPressed: () {
                  Navigator.pushReplacement(context,
                      MaterialPageRoute(builder: (context) => Technologies()));
                },*/
                label: Text('Suivant'),
                icon: Icon(Icons.navigate_next),
              ),
              alignment: Alignment.bottomRight,
            ),
            ],
          ),
        ),
    );
  }
}

It is because you have missed return before new ListView.builder in FutureBuilder's builder method, so it is not returning anything but null . 这是因为你已经错过了return前, new ListView.builder在FutureBuilder的构建器方法,因此它不返回任何东西,但null

So, your updated Theme page will like this: 因此,更新的主题页面将如下所示:

  class _Theme extends State<Theme> {
     var name;
     @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Blackbox"),
        ),
        body: new Center(
          child : new Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              new Container(
                margin: EdgeInsets.only(top: 10.0, bottom: 10.0),
                child: new Text('Sélection du profil',
                    style: new TextStyle(
                        fontSize: 24, fontWeight: FontWeight.bold)),
                        alignment: Alignment.topCenter),
              new Expanded(
                child : new FutureBuilder<List<t.Theme>>(
                    future: t.Theme().getThemes(),
                    builder: (context, snapshot){
                      if (!snapshot.hasData){
                        return CircularProgressIndicator();
                      }else if(snapshot.hasError){
                        return Text("${snapshot.error}");
                      }else{
                        return ListView.builder(
                          itemCount: snapshot.data.length,
                          itemBuilder: (BuildContext context, index) {
                            for (var s in snapshot.data){
                              name = s.name;
                            }
                            return new ListTile(
                              title: new Text(name),
                            );
                          },
                        );
                      }                   
                    },
              )
              ),
              new Container(
              margin: EdgeInsets.only(right: 10.0),
              child: new RaisedButton.icon(
                /*onPressed: () {
                  Navigator.pushReplacement(context,
                      MaterialPageRoute(builder: (context) => Technologies()));
                },*/
                label: Text('Suivant'),
                icon: Icon(Icons.navigate_next),
              ),
              alignment: Alignment.bottomRight,
            ),
            ],
          ),
        ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM