I am trying to display the name of a project in a new page, as a string that I had saved in a previous page using Shared Preferences. Below is the section of code where I saved this:
onPressed: () async {
SharedPreferences localStorage =
await SharedPreferences
.getInstance();
localStorage.setString(
'project_id', nDataList.id);
localStorage.setString(
'project_name',
nDataList.title);
localStorage.setString(
'project_desc',
nDataList.description);
localStorage.setString(
'project_due',
nDataList.endDate);
// ignore: use_build_context_synchronously
Navigator.pushNamed(
context, 'activities');
},
)
In the new page, I am using the get string in a function and then I want to display the result in a text widget. Here is all the code for the second page:
import 'package:flutter/material.dart';
import 'package:mne/Actual Tasks/activity_widget.dart';
import 'package:mne/UserTasks/task_widget.dart';
import 'package:shared_preferences/shared_preferences.dart';
class ProjectTask extends StatefulWidget {
const ProjectTask({Key key}) : super(key: key);
@override
State<ProjectTask> createState() => _ProjectTaskState();
}
class _ProjectTaskState extends State<ProjectTask> {
@override
void initState() {
super.initState();
_fetchData();
}
Future<Null> _fetchData() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences localStorage = await SharedPreferences.getInstance();
var pname = localStorage.getString('project_name');
var pdesc = localStorage.getString('project_desc');
var pdue = localStorage.getString('project_due');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: true,
centerTitle: true,
title: const Text('Project Details')),
body: SingleChildScrollView(
child: Column(children: [
// for image
Container(
child: Image.asset('assets/images/projectbanner.png'),
),
//for project name
Container(
child: Row(children: [
Container(
padding: const EdgeInsets.only(right: 10, top: 8),
child: const Icon(Icons.calendar_month_outlined)),
RichText(
text: TextSpan(children: [
TextSpan(
text: 'Due: ',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.black)),
TextSpan(
text: '$pname',
style: TextStyle(fontSize: 14, color: Colors.black))
])),
])),
// for description title
Container(child: const Text('Description')),
// for actual desc
Container(),
// for task title
Container(),
// for task widget
Container(height: 630, child: const ActivityWidget()),
]),
),
);
}
}
The error that I am getting says that 'pname' is undefined and that it is of type dynamic. How can I use the information saved in the variable in the text widget? Any help is much appreciated
CodePudding user response:
Try this:
class ProjectTask extends StatefulWidget {
const ProjectTask({Key key}) : super(key: key);
@override
State<ProjectTask> createState() => _ProjectTaskState();
}
class _ProjectTaskState extends State<ProjectTask> {
String pname;
String pdesc;
String pdue;
@override
void initState() {
super.initState();
_fetchData();
}
Future<Null> _fetchData() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences localStorage = await SharedPreferences.getInstance();
setState(() {
pname = localStorage.getString('project_name');
pdesc = localStorage.getString('project_desc');
pdue = localStorage.getString('project_due');
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: true,
centerTitle: true,
title: const Text('Project Details')),
body: SingleChildScrollView(
child: Column(children: [
// for image
Container(
child: Image.asset('assets/images/projectbanner.png'),
),
//for project name
Container(
child: Row(children: [
Container(
padding: const EdgeInsets.only(right: 10, top: 8),
child: const Icon(Icons.calendar_month_outlined)),
RichText(
text: TextSpan(children: [
TextSpan(
text: 'Due: ',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.black)),
TextSpan(
text: '$pname',
style: TextStyle(fontSize: 14, color: Colors.black))
])),
])),
// for description title
Container(child: const Text('Description')),
// for actual desc
Container(),
// for task title
Container(),
// for task widget
Container(height: 630, child: const ActivityWidget()),
]),
),
);
}
}
CodePudding user response:
Try this
class ProjectTask extends StatefulWidget {
const ProjectTask({Key key}) : super(key: key);
@override
State<ProjectTask> createState() => _ProjectTaskState();
}
class _ProjectTaskState extends State<ProjectTask> {
String pname;
String pdesc;
String pdue;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
await _fetchData();
});
}
Future<Null> _fetchData() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences localStorage = await SharedPreferences.getInstance();
setState(() {
pname = localStorage.getString('project_name');
pdesc = localStorage.getString('project_desc');
pdue = localStorage.getString('project_due');
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: true,
centerTitle: true,
title: const Text('Project Details')),
body: SingleChildScrollView(
child: Column(children: [
// for image
Container(
child: Image.asset('assets/images/projectbanner.png'),
),
//for project name
Container(
child: Row(children: [
Container(
padding: const EdgeInsets.only(right: 10, top: 8),
child: const Icon(Icons.calendar_month_outlined)),
RichText(
text: TextSpan(children: [
TextSpan(
text: 'Due: ',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.black)),
TextSpan(
text: '${pname??''}',
style: TextStyle(fontSize: 14, color: Colors.black))
])),
])),
// for description title
Container(child: const Text(pdesc??'')),
// for actual desc
Container(),
// for task title
Container(),
// for task widget
Container(height: 630, child: const ActivityWidget()),
]),
),
);
}
}