页面更改时颤动中消失的数据
Posted
技术标签:
【中文标题】页面更改时颤动中消失的数据【英文标题】:disappearing data in flutter on page change 【发布时间】:2021-04-05 19:06:22 【问题描述】:我启动了 Flutter 应用程序来学习 API 功能,但我遇到了这个问题,即当我更改页面并返回上一页时,我正在显示的数据在所有页面上都变为空。我不知道这背后的原因。
下面是它的运行图像-:
下面是我从一页获取数据到主页并显示它的代码。 并在页面更改时消失-:
import 'package:education/LogIn_SignUp/Log_In.dart';
import 'package:education/Screens/Profile_Screen.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'About_Scree.dart';
import 'Change_Password.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:async';
class homeView extends StatefulWidget
var user_id;
var user_name;
var mobile_number;
var user_address;
var user_Email;
var coaching_id;
var coaching_name;
var coaching_address;
var primary_no;
var altnumber;
var coaching_email;
homeView(
this.user_id,this.user_name,this.mobile_number,this.user_address,this.coaching_id,this.coaching_name,
this.coaching_address,this.primary_no,this.altnumber,this.coaching_email,this.user_Email
);
@override
State<StatefulWidget> createState() => homeViewState();
class homeViewState extends State<homeView>
TextEditingController editingController = TextEditingController();
var showItemList = List<String>();
int count = 10;
var userid, username, mobilenumber, useraddress, userEmail, coachingid, coachingname, coachingaddress;
var primaryno, alt_number, coachingemail;
void setdata()
setState(()
userid = widget.user_id;
username = widget.user_name;
mobilenumber = widget.mobile_number;
useraddress = widget.user_address;
userEmail = widget.user_Email;
coachingid = widget.coaching_id;
coachingname = widget.coaching_name;
coachingaddress = widget.coaching_address;
);
@override
void initState()
// showItemList.addAll(initList);
// getData();
setdata();
super.initState();
@override
void dispose()
editingController.dispose();
super.dispose();
/// -----------------------------------------------------------------------------------------------------------------------------
/// Main Build Function for the class.
/// -----------------------------------------------------------------------------------------------------------------------------
@override
Widget build(BuildContext context)
return SafeArea(
child: Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(90.0),
child: AppBar(
title: Padding(
padding: const EdgeInsets.only(top: 20),
child: Text(
'$coachingname',//'$widget.coaching_name',
style: TextStyle(
fontSize: 25.0,
letterSpacing: 4.5,
fontFamily: 'Sarif',
fontWeight: FontWeight.w300,
color: Colors.white,
),
),
),
backgroundColor: Colors.deepPurpleAccent,
centerTitle: true,
actions: <Widget>[
Column(
children: [
InkWell(
child: new IconButton(
icon: Padding(
padding: const EdgeInsets.only(top: 20),
child: Icon(Icons.exit_to_app),
),
onPressed:()
Navigator.pushReplacement(context, MaterialPageRoute(
builder: (context) => LoginPage(),
));
),
onTap: ()
Navigator.pushReplacement(context, MaterialPageRoute(
builder: (context) => LoginPage()
));
,
),
],
),
],
),
),
drawer: Drawer(
child: Container(
padding: EdgeInsets.all(10),
child: ListView(
children: <Widget>[
Container(
height: 180,
decoration: BoxDecoration(
gradient: LinearGradient(colors: [
Colors.purple.shade300,
Colors.deepPurpleAccent.shade200,
]),
),
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 45.0, bottom: 10),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(55),
),
color: Colors.blue,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Icon(Icons.perm_identity_outlined, color: Colors.white,size: 30),
)
),
),
Text(
'$username',//'$widget.user_name',
style: TextStyle(
color: Colors.white,
fontSize: 24,
letterSpacing: 2.5,
),
),
],
),
),
ListTile(
title: Text('Profile',
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w400,
letterSpacing: 1.5,
),
),
leading: Icon(Icons.person_pin, color: Colors.blue,),
selectedTileColor: Colors.blue,
onTap: ()
Navigator.pushReplacement(context, MaterialPageRoute(
builder: (context) => ProfilePage(
user_id: userid, user_name:username, mobile_number:mobilenumber, user_address:useraddress,
user_Email: userEmail,
)
));
,
),
ListTile(
title: Text('Change Password',
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w400,
letterSpacing: 1.5,
),
),
leading: Icon(Icons.security, color: Colors.blue,),
selectedTileColor: Colors.blue,
onTap: ()
Navigator.pushReplacement(context, MaterialPageRoute(
builder: (context) => ChangePassword()
));
,
),
ListTile(
title: Text('About',
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w400,
letterSpacing: 1.5,
),
),
leading: Icon(Icons.info_outline, color: Colors.blue,),
selectedTileColor: Colors.blue,
onTap: ()
Navigator.pushReplacement(context, MaterialPageRoute(
builder: (context) => AboutScreen(
coaching_id:coachingid, coaching_name:coachingname, coaching_address:coachingaddress,
primary_no:primaryno, altnumber:alt_number, coaching_email:coachingemail
)//coaching details
));
,
),
ListTile(
title: Text(
'Rate',
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w400,
letterSpacing: 1.5,
),
),
leading: Icon(Icons.star_border_outlined, color: Colors.blue,),
selectedTileColor: Colors.blue,
onTap: ()
,
),
Divider(
color: Colors.red,
thickness: 1,
),
SizedBox(height: 10,),
Text(
'Contact Us',
style: TextStyle(
fontSize: 20,
color: Colors.grey[800],
letterSpacing: 1.5,
fontWeight: FontWeight.w500,
),
),
SizedBox(height: 10,),
ListTile(
title: Text('Share',
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w400,
letterSpacing: 1.5,
),
),
leading: Icon(Icons.share, color: Colors.blue,),
selectedTileColor: Colors.blue,
onTap: ()
,
),
ListTile(
title: Text('Privacy Policy',
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w400,
letterSpacing: 1.5,
),
),
leading: Icon(Icons.privacy_tip, color: Colors.blue,),
selectedTileColor: Colors.blue,
onTap: ()
,
),
ListTile(
title: Text(
'LogOut',
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w400,
letterSpacing: 1.5,
),
),
leading: Icon(Icons.logout, color: Colors.blue,),
selectedTileColor: Colors.blue,
onTap: ()
Navigator.pushReplacement(context, MaterialPageRoute(
builder: (context) => LoginPage()
));
,
),
],
),
),
),
body: Container(),
),
);
我传递给这个页面的数据如下 -:
I/flutter ( 4512): address: jaipur, jaipur, alt_number: 9865358577, coach_address: Gautam Budha nagar, noida, coach_id: 2, coach_name: SSG Coaching, coaching_email: SSG@gmail.com, mobile_number: 9999999999, primary_no: 9898557898, res: success, user_id: 3, user_mail: s@gmail.com, user_name: sagar, user_pass: sagar123
请帮忙,因为我无法追踪错误或找到任何解决方案。
【问题讨论】:
表示您想假设您从 MainPage 转到 homeView 页面然后显示数据,但是当您从 homeView 转到另一个页面并返回到另一个页面时,数据显示不正确? 是的,每当我更改页面时,数据就会消失 是的,因为您通过构造函数仅从一页发送数据。所以这个数据消失了 【参考方案1】:这是因为您使用的是 Navigator.pushReplacement 而不是 Navigator.push。当您使用 pushReplacement 时,当前路由会被新路由覆盖。
所以用这个:
Navigator.push(context, MaterialPageRoute(
builder: (context) => ProfilePage(
user_id: userid, user_name:username, mobile_number:mobilenumber, user_address:useraddress,
user_Email: userEmail,
)
));
在 ProfilePage 中,使用
Navigator.pop(context);
返回主视图
【讨论】:
【参考方案2】:这很正常,因为您的数据没有存储在持久性存储中,如 SQL lite、共享首选项等。而且您不应该将任何不重要的东西保存到持久性本地存储中,因为这对于您的应用程序大小来说太贵了。还有更多的东西是状态管理和状态管理,您不需要将数据存储在持久存储中,而不是将数据存储在临时的所谓“半持久”状态中。如果您想了解更多有关管理数据的信息,而无需疯狂地进行异步活动,您可以按照以下步骤操作。
首先是使用 vanilla Navigator push 和类构造函数传递数据(取决于您的路由类型“命名或非 ClassNamed 类型的路由”) 就像提到的@dshukertjr 一样
Navigator.push(context, MaterialPageRoute(
builder: (context) => ProfilePage(user_id: userid, user_name:username, mobile_number:mobilenumber, user_address:useraddress,user_Email: userEmail,)
));
或者改用命名路由
Navigator.pushNamed(context, PackageWeight.routeName, arguments: Your data here);
在这里了解命名路线: https://flutter.dev/docs/cookbook/navigation/named-routes
优点:这很简单, 缺点:您不能仅将数据传递给整个应用程序,因为这不足以管理许多小部件的状态。二是使用提供者/状态管理将当前数据保存在本地了解状态管理。 提示:可以使用provider包,很简单,加个map类解析数据 https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple
优点:这对于管理许多小部件的状态非常好。 缺点:有时使用提供者和消费者来执行简单的任务(例如保存按钮保存等)有点过头了 个人建议:学习这个,你不会后悔的,因为你几乎可以在任何地方和任何情况下使用它来管理状态和数据(半持久化)。【讨论】:
非常感谢您提供的信息,我会阅读的以上是关于页面更改时颤动中消失的数据的主要内容,如果未能解决你的问题,请参考以下文章