Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

flutter - Store List<Obj> to SharedPreferences

I want to store List<WorkForceArguments> to SharedPreferences.

import 'dart:convert';
 
class WorkForceArguments {
  var name;
  var no;
  WorkForceArguments({this.name, this.no});
 
  factory WorkForceArguments.fromJson(String str) =>
      WorkForceArguments.fromMap(json.decode(str));
 
  String toJson() => json.encode(toMap());
 
  factory WorkForceArguments.fromMap(Map<String, dynamic> json) =>
      WorkForceArguments(
        name: json["name"],
        no: json["no"],
      );
 
  Map<String, dynamic> toMap() => {
        "name": name,
        "no": no,
      };
}
 

In pageB, when submit button is clicked, it will pop the data back to page A

 Widget _showButton() {
    return Container(
      width: double.infinity,
      height: 40,
      child: MaterialButton(
        color: Theme.of(context).primaryColor,
        onPressed: () {
          Navigator.pop(
              context,
              WorkForceArguments(
                  _workForceController.text, _numberController.text));  // Error!
        },
        child: Text(
          Localization.of(context).submit,
          style: TextStyle(fontSize: 15.0, color: Colors.white),
        ),
      ),
    );
  }

And in page A, it has a store button to store data to SharedPreferences. But I get error on PageB.

  Navigator.pop(
                  context,
                  WorkForceArguments(
                      _workForceController.text, _numberController.text));  // Error too many arguments, expect 0 but found 2
question from:https://stackoverflow.com/questions/65905549/store-listobj-to-sharedpreferences

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
 Navigator.pop(
  context,
  WorkForceArguments(
    name: _workForceController.text,
    no: _numberController.text,
  ),
)

You tried to add arguments to constructor with named parameters, read more about arguments to constructors and methods in Dart here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...