0

我正在尝试在颤振中创建一个注册页面。使用 HTTP 包将数据发布到后端,然后我收到状态消息。我需要访问存储在 API 缓存中的 session_id(和 csrf 值),以便我可以在共享首选项中设置其值以进行进一步的会话管理。我发布数据的代码是:

 Future registeruser(String firstname, String lastname,String email,String password,int 
 phone) async {
  String apiUrl="api here";
  final body ={"firstname":firstname,
    "lastname":lastname,
    "email":email,
    "password":password,
    "phone":phone,

  };
  final response= await http.post(apiUrl,headers:{'Content-type': 
 'Application/json','Accept':'Application/json'} ,body:body );
  var convertedDatatoJson= jsonDecode(response.body);
  return convertedDatatoJson;

 }


 var res =await registeruser(firstname, lastname, email, password, phone); 
 if(res.containsKey('status')){
                              setState(() {
                                message=res['status'];
                              });
                              if(res['status']==1){

                                 Navigator.pop(context);
                              }
                              else{
                                print('error');
                              }
                            }
                            }}, 

但是我如何从 API 的缓存中获取数据并存储在共享首选项中,我对颤振很陌生,请帮助我。

4

1 回答 1

0

如果您使用的是 Laravel,请确保您的 registeruser 调用的端点返回会话和 csrf:

function register(Request $request) {
   return([ 
      "session" => $request->session()->get('key') ,
      "csrf" => csrf(),
   ]);
}

在 Flutter 上,照常进行注册

Map res = registeruser(...);

对于用户会话,请查看FlutterSession。该软件包在 Flutter 中添加了用户会话支持,并且易于使用。

// Store value to session
await FlutterSession().set("session", res["session"]);
await FlutterSession().set("csrf", res["csrf"]);

// Retrieve item from session
dynamic token = await FlutterSession().get("session");
dynamic token = await FlutterSession().get("csrf");
于 2020-07-24T04:18:53.830 回答