通過SharedPreferences可以實(shí)現(xiàn)Flutter和native的數(shù)據(jù)共享,從而避免了頻繁的通信。
1.Flutter保存數(shù)據(jù)
SharedPreferences _prefs =await SharedPreferences.getInstance();
_prefs.setString("guid", contactInfo.name);
2.native(Android)獲取數(shù)據(jù)
SharedPreferences share=context.getSharedPreferences("FlutterSharedPreferences",Activity.MODE_PRIVATE);
String guid=share.getString("flutter.guid","");
從native的調(diào)用方式可以看出,F(xiàn)lutter將數(shù)據(jù)存儲(chǔ)在FlutterSharedPreferences肃晚,并在每個(gè)key前加上了flutter.