Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 需要了解Flatter_bloc如何注入bloc的帮助吗_Flutter_Bloc_Flutter Bloc - Fatal编程技术网

Flutter 需要了解Flatter_bloc如何注入bloc的帮助吗

Flutter 需要了解Flatter_bloc如何注入bloc的帮助吗,flutter,bloc,flutter-bloc,Flutter,Bloc,Flutter Bloc,据我目前所知,在runApp方法级别提供一个bloc可以使它在所有应用程序中都可用。我错了吗 我有这个实现 我的主.dart文件 void main() => runApp( RepositoryProvider<AuthenticationRepository>( create: (context) { return AuthenticationService(); }, child: Multi

据我目前所知,在runApp方法级别提供一个bloc可以使它在所有应用程序中都可用。我错了吗

我有这个实现

我的主.dart文件

void main() => runApp(
      RepositoryProvider<AuthenticationRepository>(
        create: (context) {
          return AuthenticationService();
        },
        child: MultiBlocProvider(
          providers: [
            BlocProvider<AuthenticationBloc>(
              create: (context) {
                final authService =
                    RepositoryProvider.of<AuthenticationRepository>(context);
                return AuthenticationBloc(authService)..add(AppLoaded());
              },
            ),
            BlocProvider<SignupBloc>(
              create: (context) {
                final authService =
                    RepositoryProvider.of<AuthenticationRepository>(context);
                return SignupBloc(authService);
              },
            )
          ],
          child: MyApp(),
        ),
      ),
    );

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
        builder: (context, state) {
          if (state is AuthenticationSuccess) {
            return MainScreen(user: state.user);
          }
          return LoginScreen();
        },
      ),
    );
  }
}
void main()=>runApp(
寄存提供者(
创建:(上下文){
返回AuthenticationService();
},
子对象:MultiBlocProvider(
供应商:[
BlocProvider(
创建:(上下文){
最终授权服务=
RepositoryProvider.of(上下文);
返回AuthenticationBloc(authService)…添加(apploated());
},
),
BlocProvider(
创建:(上下文){
最终授权服务=
RepositoryProvider.of(上下文);
返回SignupBloc(authService);
},
)
],
子项:MyApp(),
),
),
);
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:BlocBuilder(
生成器:(上下文、状态){
如果(状态为AuthenticationSuccess){
返回主屏幕(用户:state.user);
}
返回LoginScreen();
},
),
);
}
}
然后我有一个小部件:

import 'dart:developer';

import 'package:myapp/features/signup/bloc/signup_bloc.dart';
import 'package:myapp/shared/ui/textStyles.dart';
import 'package:myapp/shared/ui/widgets.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

class ProfileInfoForm extends StatefulWidget {
  @override
  _ProfileInfoFormState createState() => _ProfileInfoFormState();
}

class _ProfileInfoFormState extends State<ProfileInfoForm> {
  String _firstname, _lastname, _city, _country, _gender = "";
  GlobalKey<FormState> _key = GlobalKey<FormState>();

  List<bool> _selections = List.generate(2, (index) => false);

  @override
  Widget build(BuildContext context) {
    final _signupBloc = Provider.of<SignupBloc>(context);

    return BlocListener<SignupBloc, SignupState>(
      listener: (context, state) {
        if (state is CreateAccountFailure) {
          log("message");
        }
      },
      child: BlocBuilder(
        builder: (context, state) {
          if (state is CreateAccountLoading) {
            return CircularProgressIndicator();
          }
          return SingleChildScrollView(
            child: Container(
              child: Center(
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Form(
                    key: _key,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(
                          height: 250,
                        ),
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: CustomTextField(
                            hintText: "Firstname",
                            secured: false,
                            onSave: (value) {
                              setState(() {
                                _firstname = value;
                              });
                            },
                            onValidate: (value) {
                              if (value == "") {
                                return "Please enter your firstname";
                              }
                            },
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: CustomTextField(
                            hintText: "Lastname",
                            secured: false,
                            onSave: (value) {
                              setState(() {
                                _lastname = value;
                              });
                            },
                            onValidate: (value) {
                              if (value == "") {
                                return "Please enter your lastname";
                              }
                            },
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Row(
                            children: [
                              Expanded(
                                child: Text(
                                  "Select your gender",
                                  style: blackSubTitle,
                                ),
                              ),
                              ToggleButtons(
                                fillColor: Colors.white,
                                renderBorder: true,
                                color: Colors.black,
                                selectedBorderColor: Colors.redAccent,
                                borderRadius: BorderRadius.circular(20),
                                children: [
                                  Container(
                                    alignment: Alignment.center,
                                    // decoration: BoxDecoration(borderRadius: BorderRadius.circular(20)),
                                    width: ScreenUtil().setWidth(220),
                                    child: Text(
                                      "Female",
                                      style: blackSubTitle,
                                    ),
                                  ),
                                  Container(
                                    alignment: Alignment.center,
                                    width: ScreenUtil().setWidth(220),
                                    child: Text(
                                      "Male",
                                      style: blackSubTitle,
                                    ),
                                  ),
                                ],
                                isSelected: _selections,
                                onPressed: (index) {
                                  setState(() {
                                    _selections =
                                        List.generate(2, (index) => false);
                                    _selections[index] = !_selections[index];

                                    if (_selections.elementAt(0) == true) {
                                      setState(() {
                                        _gender = "Female";
                                      });
                                    }
                                    if (_selections.elementAt(1) == true) {
                                      setState(() {
                                        _gender = "Male";
                                      });
                                    }
                                  });
                                },
                              )
                            ],
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: FlatButton(
                            onPressed: () {
                              if (_key.currentState.validate()) {
                                _key.currentState.save();
                                // _signupBloc.add(
                                //   InitProfilePressed(
                                //       firstname: _firstname,
                                //       lastname: _lastname,
                                //       gender: _gender),
                                // );
                              } else {}
                            },
                            child: Row(
                              children: [
                                Expanded(
                                  child: Container(
                                    alignment: Alignment.center,
                                    width: 200,
                                    height: 60,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(30),
                                      color: Colors.black,
                                    ),
                                    child: Text(
                                      "Continue",
                                      style: whiteSubTitle,
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
导入“dart:developer”;
导入“包:myapp/features/signup/bloc/signup_bloc.dart”;
导入“package:myapp/shared/ui/textStyles.dart”;
导入“package:myapp/shared/ui/widgets.dart”;
进口“包装:颤振/材料.省道”;
进口“包装:颤振团/颤振团.飞镖”;
导入“package:flatter_screenutil/flatter_screenutil.dart”;
导入“包:provider/provider.dart”;
类ProfileInfoForm扩展StatefulWidget{
@凌驾
_ProfileInfoFormState createState()=>ProfileInfoFormState();
}
类_ProfileInfoFormState扩展状态{
字符串_firstname、_lastname、_city、_country、_gender=“”;
GlobalKey _key=GlobalKey();
List _selections=List.generate(2,(index)=>false);
@凌驾
小部件构建(构建上下文){
final _signupBloc=Provider.of(上下文);
返回BlocListener(
侦听器:(上下文、状态){
如果(状态为CreateAccountFailure){
日志(“消息”);
}
},
孩子:BlocBuilder(
生成器:(上下文、状态){
如果(状态为CreateAccountLoading){
返回循环ProgressIndicator();
}
返回SingleChildScrollView(
子:容器(
儿童:中心(
孩子:填充(
填充:常数边集全部(8.0),
孩子:表格(
键:_键,
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
大小盒子(
身高:250,
),
填充物(
填充:常数边集全部(8.0),
子项:CustomTextField(
hintText:“名字”,
安全:错误,
onSave:(值){
设置状态(){
_firstname=值;
});
},
onValidate:(值){
如果(值==“”){
返回“请输入您的名字”;
}
},
),
),
填充物(
填充:常数边集全部(8.0),
子项:CustomTextField(
hintText:“姓氏”,
安全:错误,
onSave:(值){
设置状态(){
_lastname=值;
});
},
onValidate:(值){
如果(值==“”){
返回“请输入您的姓氏”;
}
},
),
),
填充物(
填充:常数边集全部(8.0),
孩子:排(
儿童:[
扩大(
子:文本(
“选择您的性别”,
风格:黑色字幕,
),
),
切换按钮(
fillColor:Colors.white,
渲染器顺序:对,
颜色:颜色,黑色,
selectedBorderColor:Colors.redAccent,
边界半径:边界半径。圆形(20),
儿童:[
容器(
对齐:对齐.center,
//装饰:盒子装饰(边框半径:边框半径。圆形(20)),
宽度:ScreenUtil().setWidth(220),
子:文本(
“女性”,
风格:黑色字幕,
),
),
    BlocProvider.of() called with a context that does not contain a Bloc of type Bloc<dynamic, dynamic>.

    No ancestor could be found starting from the context that was passed to BlocProvider.of<Bloc<dynamic, dynamic>>().

    This can happen if the context you used comes from a widget above the BlocProvider.

    The context used was: BlocBuilder<Bloc<dynamic, dynamic>, dynamic>(dirty, state: _BlocBuilderBaseState<Bloc<dynamic, dynamic>,
child: BlocBuilder<SignupBloc, SignupState>(
    return BlocConsumer<SignupBloc, SignupState>(
      listener: (context, state) {
        if (state is CreateAccountFailure) {
          log("message");
        }
      },
      builder: (context, state) {
        if (state is CreateAccountLoading) {
          return CircularProgressIndicator();
        }