【问题标题】:bloc sink was called null块水槽被称为空
【发布时间】:2020-01-01 01:30:10
【问题描述】:

我使用了 imagePicker。添加后我需要分配个人资料照片。上次我使用 setState() 但没有工作。

我的 bloc 类是这样的,

class ImageBloc {
  final _loadingController = StreamController<String>();
  Stream<String> get loadingStream => _loadingController.stream;

  void setIsLoading(String image) => _loadingController.add(image);

  dispose() {
    _loadingController.close();
  }
}

播放器类 这个类是StatefullWidget

class _PlayersState extends State<Players> {
   ImageBloc bloc;
...
drawer: drawer(context, onProfilePictureChanged,bloc),
}

抽屉这是一个普通的抽屉。我在其他课程中使用了这种方法。其他类,我将 null 传递给 bloc 参数

Widget drawer(BuildContext context, onProfilePictureChanged,ImageBloc bloc) {
  return Drawer(
    child: Container(child:
//I checked bloc null or not because others class I passed null value to bloc
                    bloc == null ? Text("null value",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 13.0,
                        fontWeight: FontWeight.w500,
                      )):
//Player class only I passed bloc
                    StreamBuilder<String>(
                      stream: bloc.loadingStream,
                        initialData: "bloc value",
                        builder: (context,snapshot){
                          return Text(snapshot.data,
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 13.0,
                                fontWeight: FontWeight.w500,
                              ));
                    }),))

我通过 bloc 设置值来下沉..

 await showDialog(
        context: context,
        builder: (BuildContext context) {
          return ProfilePop(bloc:bloc);
        });

ProfilePop 类

class ProfilePop extends StatefulWidget {
  const ProfilePop({Key key, @required this.bloc}) : super(key: key);
  final ImageBloc bloc;
}

class ProfilePopState extends State<ProfilePop>{

  upload(File imageFile) async {
   ...
      setState(() {
        global.profileImage = uploadedImage["Photo"];
        widget.bloc.setIsLoading(uploadedImage["Photo"]);
      });
...
  @override
  Widget build(BuildContext context) {
    void onCancelPressed() {
      Navigator.pop(context);
    }

    return new AlertDialog(
      title:global.image == null
                    ? profile
                    : new Container(
                       ...
                      ),
        ),
      ),);}
});}}

【问题讨论】:

    标签: flutter dart bloc


    【解决方案1】:

    我没有看到您是否初始化了您的集团。例如,在 Player 类上 传递给抽屉的 bloc 始终为 null。

    你做的这个设置很复杂,也许你应该考虑使用像 bloc_provider 这样的辅助包来提供 bloc。

    【讨论】:

    • Drawer 是所有页面的通用类。使用警报添加图像时如何更改抽屉上的文本?
    • 为什么 bloc 传空?
    • 从那以后你就已经完成了一半。由于您已经在使用 bloc 和流提供程序。你可以将你的图像放到你的 ImageBloc 上,然后在流提供者快照上获取它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多