Securrency-OSS / mirai

Mirai is a Server-Driven UI (SDUI) library for Flutter. Mirai allows you to build beautiful cross-platform applications with JSON in real time.
https://pub.dev/packages/mirai
MIT License
507 stars 60 forks source link

fix: i can't send data to api and get data value #224

Open the-best-is-best opened 10 months ago

the-best-is-best commented 10 months ago
{
    "type": "padding",
    "padding": {
        "left": 12,
        "right": 12,
        "top": 12,
        "bottom": 12
    },
    "child": {
        "type": "scrollView",
        "child": {
            "type": "form",
            "child": {
                "type": "column",
                "children": [
                    {
                        "type": "textFormField",
                        "id": "name",
                        "maxLines": 1,
                        "keyboardType": "text",
                        "textInputAction": "done",
                        "textAlign": "start",
                        "textCapitalization": "none",
                        "textDirection": "ltr",
                        "textAlignVertical": "top",
                        "decoration": {
                            "hintText": "What do people call you?",
                            "filled": true,
                            "icon": {
                                "type": "icon",
                                "iconType": "cupertino",
                                "icon": "person_solid",
                                "size": 24
                            },
                            "labelText": "Name*"
                        },
                        "inputFormatters": [
                            {
                                "type": "allow",
                                "rule": "[a-zA-Z]"
                            }
                        ],
                        "validatorRules": [
                            {
                                "rule": "isName",
                                "message": "Enter a valid name"
                            }
                        ],
                        "autovalidateMode": "onUserInteraction",
                        "readOnly": false,
                        "enabled": true
                    },
                    {
                        "type": "sizedBox",
                        "height": 24
                    },
                    {
                        "type": "textFormField",
                        "id": "password",
                        "compareId": "confirmPassword",
                        "maxLines": 1,
                        "keyboardType": "text",
                        "textInputAction": "done",
                        "textAlign": "start",
                        "textCapitalization": "none",
                        "textDirection": "ltr",
                        "textAlignVertical": "top",
                        "obscureText": true,
                        "decoration": {
                            "filled": true,
                            "icon": {
                                "type": "icon",
                                "iconType": "material",
                                "icon": "password",
                                "size": 24
                            },
                            "suffixIcon": {
                                "type": "icon",
                                "iconType": "cupertino",
                                "icon": "eye",
                                "size": 24
                            },
                            "labelText": "Password*"
                        },
                        "validatorRules": [
                            {
                                "rule": "isPassword",
                                "message": "Enter a valid password"
                            }
                        ],
                        "autovalidateMode": "onUserInteraction",
                        "readOnly": false,
                        "enabled": true
                    },
                    {
                        "type": "sizedBox",
                        "height": 24
                    },
                    {
                        "type": "formField",
                        "child": {
                            "type": "elevatedButton",
                            "child": {
                                "type": "text",
                                "data": "Submit",
                                "style": {
                                    "color": "#ffffff"
                                }
                            },
                            "style": {
                                "padding": {
                                    "top": 8,
                                    "left": 24,
                                    "right": 24,
                                    "bottom": 8
                                }
                            },
                            "onPressed": {
                                "actionType": "request",
                                "url": "xxxxx/Login",
                                "method": "post",
                                "data": {
                                    "name": "{name}",
                                    "password": "{password}"
                                }
                            }
                        }
                    }
                ]
            }
        }
    }
}

I can't send the value text form field and get the result can help me or solve that thanks

i-asimkhan commented 10 months ago

Thank you for creating this @the-best-is-best At the moment we cannot pass values from values dynamically and recreate an API payload, though it is under process and very soon it will get released in the next version of Mirai.

At the moment requesting a response from the server using MiraiRequest is only possible with statically defined defines values in the JSON like this:

"onPressed":  {
  "actionType": "request",
  "url": "xxxxx/Login",
  "method": "post",
  "data": {
             "name": "name",
             "password": "password"
              }
}
the-best-is-best commented 10 months ago

i want send data user name and password after user added it so how can get value from form and get error to display or new page to go

i-asimkhan commented 10 months ago

i want send data user name and password after user added it so how can get value from form and get error to display or new page to go

Greetings @the-best-is-best Since taking the value from the form-fields (text-fields) is under the build process, the existing stable version of Mirai unfortunately cannot support this, although there is always a workaround we do not recommend this because in the next release, we are going to push major updates in this regard, including form-values and calling an API.

Workaround:

Create a custom parser just like any action parsers in MIrai let's say, FormExtractor

class FormExtractorParser extends MiraiActionParser<Map<String, dynamic>> {
  ... 

   @override
  FutureOr<dynamic> onCall(BuildContext context, Map<String, dynamic> model) {
    try {
      final values = context.read<MiraiFormCubit>().state.values;
    } catch (e) {}

  }

  ... 

}

Now pass this FormExtractorParser onto your custom parsers in Mirai.initialize like this:

await Mirai.initialize(
    actionParsers: [
      FormExtractorParser()
    ],
    parsers: const [
     ...
    ],
  );
divyanshub024 commented 10 months ago

@the-best-is-best Please keep an eye to this PR https://github.com/Securrency-OSS/mirai/pull/213

i-asimkhan commented 10 months ago

Please don't forget to check the Mirai parsers from here -> Parsers or don't hesitate to ask further.

the-best-is-best commented 10 months ago

MiraiFormCubit not found i can't access it or import it

i-asimkhan commented 10 months ago

try { final values = context.read().state.values; } catch (e) {}

@the-best-is-best #228 should fix this.

divyanshub024 commented 10 months ago

Hey everyone,

This feature is currently a work in progress (See https://github.com/Securrency-OSS/mirai/pull/213). This will be released in the upcoming v0.6. Until then kindly wait. Note: MiraiFormCubit will be removed.

the-best-is-best commented 10 months ago

until now don't understand how make button call request with data and re render new ui