Flutter pass json data to new Stateful Widget

I pass json data to new stateful widget in flutter. I can see data is not null in the debug console but stateful widget receive null data.

I tried other passing data type but still doesn't work.

How can I fix this? Any Help

Here is the code

Navigator Code

    void goToFilteredList() async {
         jsonData =await postQuotationFilteredList(
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            false,
            false,
            null,
            null);
        print(jsonData);
      await Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => FilteredQuotationList(jsonData)));


      }

Stateful Widget

class FilteredQuotationList extends StatefulWidget {
  final List<Map<String, dynamic>> jsonData;
  FilteredQuotationList(this.jsonData);

  static const String routeName = "/filteredquotationlist";
  @override
  State<StatefulWidget> createState() =>
      FilteredQuotationListScreen(this.jsonData);
}

jsonData has same type and there is the cons and this code received null data.

 class FilteredQuotationListScreen extends State<FilteredQuotationList> {
      List<Map<String, dynamic>> jsonData;

      FilteredQuotationListScreen(List<Map<String, dynamic>> jsonData) {
        this.jsonData = jsonData;
      }

      @override
      Widget build(BuildContext context) {

        print("filtreleme ekranı");

        print(jsonData);
        return Scaffold(
          body: quotationFilteredListItems(jsonData),
        );
      }

      ListView quotationFilteredListItems(List<Map<String, dynamic>> jsonData) {

        print("quotation filtered list items");

        List<GetQuotationModel> getQuotationModel =
            GetQuotationModel.fromJson(jsonData);
        print(getQuotationModel.length);
        return ListView.builder(
          itemCount: 3,
          itemBuilder: (BuildContext context, int position) {
            return Card(
              color: Colors.amberAccent,
              elevation: 2.0,
              child: ListTile(
                leading: CircleAvatar(
                  backgroundColor: Colors.green,
                  child: Text("getQuotationModel.quoteNumber"),
                ),
                title: Text("this."),
              ),
            );
          },
        );
      }
    }

Upvotes: 0

Views: 644

Answers (1)

Derek Fredrickson
Derek Fredrickson

Reputation: 752

First of all, this isn't how you should be using stateful widgets that are provided data via their constructor. Basically, the State object can access any parameter of the Widget object for free.

Here is an example of how it should be used.

class MyWidget extends StatefulWidget {
  MyWidget({
    Key key,
    @required this.myParameter,
  }) : super(key: key);

  final String myParameter;

  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  @override
  Widget build(BuildContext context) {
    return Text(widget.myParameter); // the widget accessor is included in every State
  }
}

And then using it would look like this:

@override
Widget build(BuildContext context) {
  return MyWidget(myParameter: "Hurray!");
}

Let me know if this fixes the issues you're having.

Upvotes: 1

Related Questions