方法'setStringList'在null上被调用。接收方:null尝试调用:setStringList(“ list”,_GrowableList'的实例(长度:8))。
我正在使用共享首选项来存储List,方法是使用构造函数的值获取其对象。我得到所有值,但发生以下错误:在null上调用了方法'setStringList'。接收方:null尝试调用:setStringList(“ list”,'_GrowableList'的Instance(length:8))。以下是代码。请尽快解决错误。 我会感激的!
上一页的按下功能仅供参考
onPressed: () {
_showwithdrawSnakbar(context);
setState(() {
var now = new DateTime.now();
var formatter = new DateFormat('dd-MM-yyyy');
String formatted = formatter.format(now);
if (!stopfromwith) {
print("in Withdrawer");
var routetorec=MaterialPageRoute(
builder: (context)=> Transfers(pay:Transactions(formatted,purpose:_currrentpurpose,amount:ctrlwith.text))
);
print(_currrentpurpose);
print(ctrlwith.text);
Navigator.of(context).push(routetorec);
}
});
stopfromwith = false;
}
import 'dart:convert';
import 'package:flutter/material.dart';
import 'models/Transactions.dart';
import 'package:shared_preferences/shared_preferences.dart';
List<Transactions> list = List<Transactions> ();
class Transfers extends StatefulWidget {
Transactions pay;
Transfers({Key key,this.pay}):super(key:key){
print("added here now the item from withdrawer");
print(pay);
_addTrasaction(pay);
print("IN State");
print(list);
}
@override
_TransfersState createState() => _TransfersState(pay);
}
_addTrasaction(Transactions transaction){
list.add(transaction);
print("IN _State");
print(list);//works fine
}
class _TransfersState extends State<Transfers> {
Transactions payd;
_TransfersState(this.payd);
SharedPreferences sharedPreferences;
@override
void initState() {
print(payd);//works fine
_updatelist();
initsharedPreferences();
super.initState();
}
initsharedPreferences()async{
sharedPreferences= await SharedPreferences.getInstance();
loadData();
saveData();
}
void saveData(){
List<String> stringList = list.map(
(item) => json.encode(item.toMap()
)).toList();
// sharedPreferences.remove('List');
print("it runs ");
print(stringList);//works fine
// I think the error starts from here
sharedPreferences.setStringList('list', stringList);
}
void loadData(){
List<String>llist=sharedPreferences.getStringList('list');
list=llist.map((item)=>Transactions.fromMap(json.decode(item))).toList();
setState(() { });
}
_updatelist(){
setState(() {
list.add(payd);
print(list);
saveData();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Transfers"),
centerTitle: true,
leading: Icon(Icons.repeat),
),
body: list.length == 0
? Center(child: Text("No data"))
: transfersListView(),
);
}
_subCard(Transactions transaction) {
return Dismissible(
key: Key(transaction.hashCode.toString()),
onDismissed: (direction) {
list.remove(transaction);
},
background: Container(
child: Padding(
padding: const EdgeInsets.only(left: 168.0),
child: Icon(
Icons.delete,
size: 34,
color: Colors.white,
),
),
color: Colors.red,
),
child: TFCustomCard(transaction.date,transaction.purpose,transaction.amount),
);
}
ListView transfersListView() {
return ListView.builder(
itemCount: list.length,
itemBuilder: (context, index) {
return _subCard(list[index]);
},
);
}
}
class TFCustomCard extends StatelessWidget {
String _date,_purpose,_amount;
TFCustomCard(this._date,this._purpose,this._amount);
@override
Widget build(BuildContext context) {
return Container(/*simple card with _date,_purpose,and _amount*/);
}
}