Flutter API 从一个屏幕获取数据并将其发送到另一个屏幕
Flutter API Fetch and Sending Data from One screen to another screen
当我通过 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
}
//Second Screen (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
}
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,
),
),
)
第二页屏幕内的创建构造函数
class ABC extends StatefulWidget {
@override
_ABCState createState() => _ABCState();
var id;
ABC.withId(String uid) {
id = uid;
}
}
使用 widget.id
在小部件中接受您的 ID
当我通过 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
}
//Second Screen (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
}
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,
),
),
)
第二页屏幕内的创建构造函数
class ABC extends StatefulWidget {
@override
_ABCState createState() => _ABCState();
var id;
ABC.withId(String uid) {
id = uid;
}
}
使用 widget.id
在小部件中接受您的 ID