当我通过 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
}
//第二个屏幕(启动事件)在此屏幕中有一个函数 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
关于Flutter API 获取数据并将其从一个屏幕发送到另一个屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68494228/