form_bloc
form_bloc copied to clipboard
Get Fields from restful call
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
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 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?
There should be only SelelecFieldBox
@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().
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 Sure, If you can give more information. I can give you a much more detail suggestion.
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: @.***>
Hi @aaassseee . I declared a
ListFieldBloc<SelectFieldBloc<QuestionnaireAnswerOption, Object>, dynamic>();
and then I tried with addFieldBlocs() inside onLoading(). It works great.
Thank you
@bobosette Happy to help.
@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 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.
I'll try with SingleFieldBloc