Commit 556ea774 authored by “Icebear”'s avatar “Icebear”

修复saveModel不能同步获取数据的问题

parent 838d0fe1
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart'; import 'package:fluttertoast/fluttertoast.dart';
import 'package:netrain_flutter_app/xuehao/list_page.dart'; import 'package:netrain_flutter_app/xuehao/list_page.dart';
import 'package:netrain_flutter_app/zhangfeng/TestPage.dart';
import 'laishanqi/Stateful_page.dart'; import 'laishanqi/Stateful_page.dart';
import 'laishanqi/Stateless_page.dart'; import 'laishanqi/Stateless_page.dart';
...@@ -41,6 +42,7 @@ class MyApp extends StatelessWidget { ...@@ -41,6 +42,7 @@ class MyApp extends StatelessWidget {
"login": (context) => LoginPage(), "login": (context) => LoginPage(),
"listPage":(context) => List_Page(), "listPage":(context) => List_Page(),
"LoginRequestPage":(context) => LoginRequestPage(), "LoginRequestPage":(context) => LoginRequestPage(),
"TestPage": (context) => SampleAppPage(),
"main": (context) => mainPage(), "main": (context) => mainPage(),
}, },
), ),
...@@ -90,7 +92,9 @@ class _RouterNavigatorState extends State<RouterNavigator> { ...@@ -90,7 +92,9 @@ class _RouterNavigatorState extends State<RouterNavigator> {
_item("拍照页面", PhotoApp(), 'photo'), _item("拍照页面", PhotoApp(), 'photo'),
_item("登陆", LoginPage(), 'login'), _item("登陆", LoginPage(), 'login'),
_item("登录网络请求", LoginRequestPage(), 'LoginRequestPage'), _item("登录网络请求", LoginRequestPage(), 'LoginRequestPage'),
_item("zhangfengTest页面",List_Page(),'TestPage'),
_item("listview页面",List_Page(),'listPage'), _item("listview页面",List_Page(),'listPage'),
], ],
), ),
); );
......
...@@ -10,6 +10,7 @@ class LoginApi { ...@@ -10,6 +10,7 @@ class LoginApi {
BaseHttpModel httpModel = data; BaseHttpModel httpModel = data;
UserModel userModel = UserModel.fromJson(httpModel.data); UserModel userModel = UserModel.fromJson(httpModel.data);
UserModel.saveModel(userModel); UserModel.saveModel(userModel);
print(UserModel.getModel().token);
if(onResult != null){ if(onResult != null){
onResult(data, errorMsg); onResult(data, errorMsg);
} }
......
...@@ -51,10 +51,12 @@ class _LoginState extends State<LoginRequestPage> { ...@@ -51,10 +51,12 @@ class _LoginState extends State<LoginRequestPage> {
FloatingActionButton( FloatingActionButton(
onPressed: _loginAction, onPressed: _loginAction,
child:Icon(Icons.add), child:Icon(Icons.add),
heroTag: 0,
), ),
FloatingActionButton( FloatingActionButton(
onPressed: _logoutAction, onPressed: _logoutAction,
child:Icon(Icons.add), child:Icon(Icons.add),
heroTag: 1,
), ),
], ],
), ),
......
import 'dart:convert'; import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:flustars/flustars.dart';
class SaveModel { class SaveModel {
// factory UserData() => getInstance(); // factory UserData() => getInstance();
...@@ -17,17 +17,15 @@ class SaveModel { ...@@ -17,17 +17,15 @@ class SaveModel {
// //
// } // }
static saveModel(dynamic model,String modelName) async { static saveModel(dynamic model,String modelName) {
SharedPreferences pref= await SharedPreferences.getInstance();
//将data转换成json字符串 //将data转换成json字符串
var jsonStr = json.encode(model); var jsonStr = json.encode(model);
pref.setString(modelName,jsonStr); SpUtil.putString(modelName, jsonStr);
} }
// 读取 SharedPreferences // 读取 SharedPreferences
static Future<Map> getModel(String modelName) async { static getModel(String modelName) {
SharedPreferences prefs = await SharedPreferences.getInstance(); final jsonStr = SpUtil.getString(modelName);
final jsonStr = prefs.getString(modelName);
if(jsonStr != null && jsonStr.length > 0){ if(jsonStr != null && jsonStr.length > 0){
//先将json字符串转json //先将json字符串转json
Map json = jsonDecode(jsonStr); Map json = jsonDecode(jsonStr);
...@@ -36,8 +34,7 @@ class SaveModel { ...@@ -36,8 +34,7 @@ class SaveModel {
return null; return null;
} }
static removeModel(String modelName) async{ static removeModel(String modelName){
SharedPreferences prefs = await SharedPreferences.getInstance(); SpUtil.putString(modelName,"");
prefs.setString(modelName,"");
} }
} }
\ No newline at end of file
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'dart:convert';
class SampleAppPage extends StatefulWidget{
SampleAppPage({Key key}) : super(key: key);
@override
_SampleAppPageState createState() => _SampleAppPageState();
}
class _SampleAppPageState extends State<SampleAppPage>{
String _errorText;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Sample App"),
),
body: Center(
child: TextField(
onSubmitted: (String text) {
setState(() {
if (!isEmail(text)) {
_errorText = 'Error: This is not an email';
} else {
_errorText = null;
}
});
},
decoration: InputDecoration(hintText: "This is a hint", errorText: _getErrorText()),
),
),
);
}
_getErrorText() {
return _errorText;
}
bool isEmail(String em) {
String emailRegexp =
r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
RegExp regExp = RegExp(emailRegexp);
return regExp.hasMatch(em);
}
}
\ No newline at end of file
...@@ -19,11 +19,10 @@ class UserModel{ ...@@ -19,11 +19,10 @@ class UserModel{
static UserModel getModel(){ static UserModel getModel(){
if(sharedModelInstance_ == null){ if(sharedModelInstance_ == null){
SaveModel.getModel('UserModel').then((json) => { Map json = SaveModel.getModel('UserModel');
if(json != null){ if(json != null){
sharedModelInstance_ = new UserModel.fromJson(json) sharedModelInstance_ = new UserModel.fromJson(json);
} }
});
} }
return sharedModelInstance_; return sharedModelInstance_;
} }
......
...@@ -42,7 +42,7 @@ dependencies: ...@@ -42,7 +42,7 @@ dependencies:
dio: ^4.0.0 #网络库 dio: ^4.0.0 #网络库
convert: ^3.0.1 #转码器 convert: ^3.0.1 #转码器
pull_to_refresh: ^2.0.0 pull_to_refresh: ^2.0.0
shared_preferences: ^2.0.6 flustars: ^2.0.1 #强大的工具类库
dev_dependencies: dev_dependencies:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment