To learn about Flutter, prepare to write an APP for practice. Prepare to write a public parameter in the network request section. The initial global parameter paramDic is defined as follows:
// Define some pr parameters Map<String,dynamic> paramDic = {'appType': 'flutter', 'appVersion': '1.0.3', 'androidVersion': '', 'token': UserManager.instance.token ! = null ? UserManager.instance.token : '', };Copy the code
On a network request:
var dic = paramDic;
dic['page'] = 1;
dic['size'] = 10;
Copy the code
On another network request:
var dic = paramDic;
dic['like_mark'] = 1;
dic['topic_id'] = 10;
Copy the code
As I got further along, I realized that there were more and more parameters on the interface. All of the parameters on the previous interface were paramDic, and I realized that this was a shallow copy. All the DIC I initialized were paramDic. Then I made a deep copy of paramDic;
var dic = Map<String,dynamic>.from(paramDic);
Copy the code
After checking, it was found that the token stored after login was not obtained in paramDic. The token stored in login was not obtained in paramDic. It turns out that every time
var dic = Map<String,dynamic>.from(paramDic);
Copy the code
When obtaining public parameters from paramDic, the paramDic token is not obtained in real time. The paramDic token is stored when the global object is initialized. Change paramDic to real-time fetch
Map<String,dynamic> get paramDic => {'appType': 'flutter', 'appVersion': '1.0.3', 'androidVersion': '', 'token': UserManager.instance.token ! = null ? UserManager.instance.token : '', };Copy the code
Code carefully