Flutter API 从一个屏幕获取数据并将其发送到另一个屏幕 [英] Flutter API Fetch and Sending Data from One screen to another screen

查看:18
本文介绍了Flutter API 从一个屏幕获取数据并将其发送到另一个屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过 API 登录时,我得到了 User_id 值,我可以在控制台上打印,但我想在另一个屏幕上使用它.那么如何将数据发送到另一个屏幕呢?//第一个屏幕

When I am logged in through the API I am getting the value User_id which I am able to print on console, but I want to use it on another screen. So how can I send the data to another screen? // First Screen

Future postMethod() async {
var api = Uri.parse("https://demo.likemyfiles.com/DS/api/auth/otp");
Map mapeddate = {
  'phone': _phone.text,
  'otp': _otp.text,
};
final response = await http.post(api, body: mapeddate);
print(response.body);
var res = json.decode(response.body);
print(res['user_id']); 
Navigator.pushNamed(context, StartActivity.id);   //here i want to send the User_Id
}

//第二个屏幕(开始活动)在这个屏幕中有一个函数FetchData,我要在其中使用数据

//Second Screen (Start Activity) in this screen there is a function FetchData where i want to use the data

Future fetchdata() async {
var url = await http.get(Uri.parse(
    "http://demo.likemyfiles.com/DS/api/api_supervisor/supervisor/3")); //Instead of 3 i want 
  to use the User_Id variable
 }

推荐答案

您应该尝试声明 constructor 第一页接受 id 并将此 id 推送到第二页或屏幕,如下代码是第一页

You should try to declare constructor the first page accept the id and push this id to second page or screen like this below code is first page

Navigator.push(
context,
MaterialPageRoute(
  builder: (context) => ABC.withId(
    id,
  ),
 ),
)

第二页屏幕内的创建构造函数

the create constructor inside second page screen

class ABC extends StatefulWidget {
  @override
  _ABCState createState() => _ABCState();

  var id;
  
  ABC.withId(String uid) {
    id = uid;
   
  }
}

使用 widget.id

这篇关于Flutter API 从一个屏幕获取数据并将其发送到另一个屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆