【问题标题】:Flutter API Fetch and Sending Data from One screen to another screenFlutter API 从一个屏幕获取数据并将其发送到另一个屏幕
【发布时间】:2021-09-30 06:56:11
【问题描述】:

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

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
}

//第二屏(Start Activity)在这个屏幕中有一个函数FetchData,我要在其中使用数据

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
 }

【问题讨论】:

    标签: flutter flutter-layout flutter-web flutter-animation flutter-test


    【解决方案1】:

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

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

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

    class ABC extends StatefulWidget {
      @override
      _ABCState createState() => _ABCState();
    
      var id;
      
      ABC.withId(String uid) {
        id = uid;
       
      }
    }
    

    使用 widget.id

    在小部件内接受您的 id

    【讨论】:

      猜你喜欢
      • 2019-07-18
      • 2021-01-06
      • 2019-10-27
      • 2019-05-10
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多