1

我正在尝试将数据传递到启动 API 调用的块中,但出现错误:

“只能在初始化程序中访问静态成员”

这可能吗?我的方法完全错误吗?

我也尝试将其切换postid为静态,但不确定如何实际访问postid该状态。

代码:

class ViewPost extends StatefulWidget {
   int postid; // sent from previous screen

}

ViewPost(
  {Key key,
  this.postid     
  })
  : super(key: key);
}

我需要访问postid的状态:

@override
  _ViewPostState createState() => _ViewPostState();
}

class _ViewPostState extends State<ViewPost> {


final int postid; // somehow I'd need to access postid here

  final PostBlog _PostBloc =
  PostBloc(httpClient: http.Client(), postid: postid);
}



  _ViewBlogState() { 
_postBloc.dispatch(Fetch());
}

建造:

@override
  Widget build(BuildContext context) {

return Scaffold(
  appBar: AppBar(
    title: Text(widget.name),
    backgroundColor: Colors.black,
    automaticallyImplyLeading: true,

    leading: IconButton(
      icon: Icon(Icons.arrow_back),
      onPressed: () onPressed: ()  => Navigator.pop(context, true),

    ),
  ),

  body: new RefreshIndicator(
    child: BlocProviderTree(
        blocProviders: [
          PostProvider<PostBloc>(bloc: _postBloc)

        ],
        child: BlocBuilder(
          bloc: _postBloc,
          builder: (BuildContext context, PostState state) {
          ....

  }
4

1 回答 1

0

走了不同的路线,让它工作。使用了:

_postBloc.dispatch(Fetch(postid: postid));

将其发送到事件。

然后简单地在事件本身中声明它,然后在进行 api 调用时可以在 event.postid 中访问它。

于 2019-03-30T01:36:26.240 回答