GiancarloCode / form_bloc

🔥 Dart and Flutter Package 🔥 Easy Form State Management using BLoC pattern 🔥 Wizard/stepper forms, asynchronous validation, dynamic and conditional fields, submission progress, serialization and more! 🔥
https://GiancarloCode.github.io/form_bloc/
465 stars 200 forks source link

[question] example to access API backend using form_block 0.12.0 #55

Closed hansbak closed 4 years ago

hansbak commented 4 years ago

Can somebody give me an example how to access a backend API using the latest 0.12.0 version of form_bloc? It needs already be defined in main.dart...and then implemented in the bloc I have it working in the bloc, but want it already started in main.dart so it does not restart with every screen. Thanks in advance! Hans

GiancarloCode commented 4 years ago

Hello The way I recommend is to inject your service / repository using the constructor, then you can use it whatever you want.

  MyFormBloc({
    @required Repository repository,
  })  : _repository = repository,
     {
    addFieldBlocs(
      fieldBlocs: [field1, field2, fieldN],
    );
  } 
  final Repository _repository;

  @override
  void onSubmitting() async {
    try {
      await _repository.awesomeMethod(
        field1.value,
        field2.value,
      );
      emitSuccess();
    } catch (e) {
      emitFailure();
    }
  }

If you need to fill the fields with data from backend, check this tutorial

Maybe you want to serialize the form, check out this tutorial


The recommended architecture is the same one that bloc library recommend. https://bloclibrary.dev/#/architecture


Do you have any doubt?

hansbak commented 4 years ago

woow thanks for the quick response!

hansbak commented 4 years ago

OK, i tried to implement https://bloclibrary.dev/#/architecture, the login example with your suggestion of the myFormBloc above. I am stuck how to implement this in the LoginForm which has two extends, in the https://bloclibrary.dev/#/architecture example there is just one.

In the LoginForm I have now the userRepository undefined in:

return BlocProvider(
  create: (context) => LoginFormBloc(
        authenticationBloc: BlocProvider.of<AuthenticationBloc>(context),
        userRepository: userRepository,
  ),

Would be nice to have an example combining your form_bloc and the login example in https://bloclibrary.dev/#/architecture?

regards, Hans

GiancarloCode commented 4 years ago

Hello Has two extends and the example just one? I did not understand :(, can you explain better? I'll add an example these days :),

hansbak commented 4 years ago

in your LoginForm:

class LoginForm extends StatefulWidget { @override _LoginFormState createState() => _LoginFormState(); } class _LoginFormState extends State { LoginFormBloc _formBloc; List _focusNodes;

in the https://bloclibrary.dev/#/architecture:

class LoginPage extends StatelessWidget { final UserRepository userRepository;

LoginPage({Key key, @required this.userRepository}) : assert(userRepository != null), super(key: key);

@override Widget build(BuildContext context) {

if you can write the example I am sure more people than me would find this very useful..... thanks for the fantastic form_bloc and your site at: https://giancarlocode.github.io/form_bloc/#/ probably all written in flutter is awsome!

GiancarloCode commented 4 years ago

Thank you, Although as I see, your confusion it's more about the stateful widgets and stateless widgets Check out this official flutter tutorial https://flutter.dev/docs/development/ui/interactive#stateful-and-stateless-widgets

I would also like you to tell me more specifically what you want to see in the example :)

I stay tuned :)

hansbak commented 4 years ago

Hi Gian,

what is needed here is your 'simple' example at https://giancarlocode.github.io/form_bloc/#/ extended with userRepository and Authentication bloc/states/events from https://bloclibrary.dev/#/flutterlogintutorial

thank you in advance for your help!

hansbak commented 4 years ago

Hi Gian, i think i done it, have a look at: https://github.com/hansbak/flutter_form_bloc_userRepository

let me know your comments!

regards, Hans

GiancarloCode commented 4 years ago

Yes, that's pretty good.

But in this part use a try / catch with the repository method.

  @override
  void onSubmitting() async {
    try{
      final token = await userRepository.authenticate(
        username: email.value,
        password: password.value,
      );
      authenticationBloc.add(LoggedIn(token: token));
    } catch (e) {
       // TODO: Map exception to error message 
       emitFailure(failureResponse: e.toString());
    }
  }

The other week I will upload a complete project and you can review, but you already have the general idea :)

hansbak commented 4 years ago

Thanks for your comment!

Will review your version!

eugene123tw commented 4 years ago

I was also trying to figure out how to pass Authentication repository to form BLoC and this helped me a lot!