代码之家  ›  专栏  ›  技术社区  ›  Hesam

为什么FutureBuilder在flutter没有收到呼叫?

  •  0
  • Hesam  · 技术社区  · 6 年前

    _fetchPost() 每当我点击它。此方法调用 fetchPost() var post = ... 行。但是,我的问题是 builder

    我是一个新手,所以我的代码是错误的吗?

    void _fetchPost() {
        FutureBuilder<Post>(
          future: fetchPost(),
          builder: (BuildContext context, AsyncSnapshot<Post> snapshot) {
            if (snapshot.hasData) {
              String res = snapshot.data.parm1.toString() + snapshot.data.op + snapshot.data.parm2.toString();
              setState(() {
                _result = res;
              });
            } else if (snapshot.hasError) {
              setState(() {
                _result = snapshot.error;
              });
            }
    
            // By default, show a loading spinner
            return CircularProgressIndicator();
          },
        );
      }
    
      Future<Post> fetchPost() async {
        final response = await http.get('http://test.ethorstat.com/test.ashx');
        if (response.statusCode == 200) {
          // If server returns an OK response, parse the JSON
          var post = Post.fromJason(json.decode(response.body));
          return post;
        } else {
          // If that response was not OK, throw an error.
          throw Exception('Failed to load post!');
        }
      }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   creativecreatorormaybenot    6 年前

    FutureBuilder 是一个 首先将其插入小部件树

    未来建筑 build

    class FetchWidget extends StatelessWidget {
    
      @override
      Widget build(BuildContext context) => FutureBuilder<Post>(
        future: fetchPost(),
        builder: (BuildContext context, AsyncSnapshot<Post> snapshot) {
          if (snapshot.hasData) {
            String res = snapshot.data.parm1.toString() + snapshot.data.op + snapshot.data.parm2.toString();
            setState(() { // setState will not work here, I only used this StatelessWidget to show what I mean with inserting it into the build tree
              _result = res;
            });
          } else if (snapshot.hasError) {
            setState(() {
              _result = snapshot.error;
            });
          }
    
          // By default, show a loading spinner
          return CircularProgressIndicator();
        },
      );
    
      Future<Post> fetchPost() async {
        final response = await http.get('http://test.ethorstat.com/test.ashx');
        if (response.statusCode == 200) {
          // If server returns an OK response, parse the JSON
         var post = Post.fromJason(json.decode(response.body));
         return post;
        } else {
          // If that response was not OK, throw an error.
         throw Exception('Failed to load post!');
        }
      }
    }
    

    这不会编译,因为我只将您的代码复制到 StatelessWidget setState “将[…]添加到小部件树”

    或者你可以用普通的 async await

    void _fetchPost() async {
      final Post fetchedPost = await fetchPost();
      final String res = fetchedPost.parm1.toString() + fetchedPost.op + fetchedPost.parm2.toString();
      setState(() {
        _result = res;
      });
    }
    

    fetchPost ,但是如果你想使用 builder .