form_bloc icon indicating copy to clipboard operation
form_bloc copied to clipboard

Get Fields from restful call

Open bobosette opened this issue 2 years ago • 11 comments

Hi everybody. I need to implement a questionnaire inside my app and I get questions and answers from a restful call, so when I initialize fields I don't know exactly how many they will be and their type, but i will get these informations when i will make the call inside the onLoading() method

`

@override void onLoading() async { try { await Future.delayed(Duration(milliseconds: 1500));

 final questionnaire = await http.get(Uri.parse('https://myUrl.com'));

 need to build the form

  emitLoaded();
} catch (e) {
 

  emitLoadFailed();
}

}

` How can I dinamically add fields to my FormBloc directly from the onLoading() method?

bobosette avatar Jun 06 '22 06:06 bobosette

@bobosette It seems the scope is too large. Would you like to share a sample JSON with what questionnaire will return? It that only simple text field or with complex multiple selection form?

aaassseee avatar Jun 06 '22 11:06 aaassseee

There should be only SelelecFieldBox

bobosette avatar Jun 06 '22 11:06 bobosette

@bobosette It is hard to do example if you don't provide any runnable code. Lacking with information I can only provide a rough idea to help you. Lets say we have a JSON list with object contain type and options, you can generate the field blocs by lopping the object list and create list of field blocs. After constructing the field bloc you can add the list of field bloc by addFieldBlocs function before emitLoaded().

aaassseee avatar Jun 06 '22 12:06 aaassseee

This could be a good option. I'll try and I'll let you know. Sorry for the lack of code

Il Lun 6 Giu 2022, 14:57 Jack Liu @.***> ha scritto:

@bobosette https://github.com/bobosette It is hard to do example if you don't provide any runnable code. Lacking with information I can only provide a rough idea to help you. Lets say we have a JSON list with object contain type and options, you can generate the field blocs by lopping the object list and create list of field blocs. After constructing the field bloc you can add the list of field bloc by addFieldBlocs function before emitLoaded().

— Reply to this email directly, view it on GitHub https://github.com/GiancarloCode/form_bloc/issues/310#issuecomment-1147417944, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADEWB4NRKZC7MBKMXVATKDDVNXYVDANCNFSM5X6K7K7A . You are receiving this because you were mentioned.Message ID: @.***>

bobosette avatar Jun 06 '22 13:06 bobosette

@bobosette Sure, If you can give more information. I can give you a much more detail suggestion.

aaassseee avatar Jun 06 '22 13:06 aaassseee

When I'll come back to the laptop, I'll try and if needed I'll provided more information

Il Lun 6 Giu 2022, 15:07 Jack Liu @.***> ha scritto:

@bobosette https://github.com/bobosette Sure, If you can give more information. I can give you a much more detail suggestion.

— Reply to this email directly, view it on GitHub https://github.com/GiancarloCode/form_bloc/issues/310#issuecomment-1147426449, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADEWB4OBLHS4I36EVJXV653VNXZXNANCNFSM5X6K7K7A . You are receiving this because you were mentioned.Message ID: @.***>

bobosette avatar Jun 06 '22 13:06 bobosette

Hi @aaassseee . I declared a ListFieldBloc<SelectFieldBloc<QuestionnaireAnswerOption, Object>, dynamic>();
and then I tried with addFieldBlocs() inside onLoading(). It works great. Thank you

bobosette avatar Jun 07 '22 14:06 bobosette

@bobosette Happy to help.

aaassseee avatar Jun 07 '22 14:06 aaassseee

@aaassseee I have another question for you: What if I should have different FieldBloc inside my list (SelectFieldBloc, TextFieldBloc, etc....)?? Is it possible to declared a ListFieldBloc<dynamic, dynamic>() or I don't something similar? It would be great to create a dynamic form.

bobosette avatar Jun 08 '22 15:06 bobosette

@bobosette dynamic is not the best options if there is a base type. For the field bloc except ListFieldBloc and GroupFieldBloc you can use SingleFieldBloc as their base type. If you include ListFieldBloc and GroupFieldBloc in your list of bloc, you can use FieldBloc as your base type.

aaassseee avatar Jun 09 '22 01:06 aaassseee

I'll try with SingleFieldBloc

bobosette avatar Jun 09 '22 07:06 bobosette