kyed
kyed

Reputation: 61

Flutter - Navigator.push value to list

I have a Navigator.push in a widget, that parse a value to a new widget. That is working. In the Stateful widget that I parse the value, I am trying to add the value to a list, but get an error here:

Producerlist.add (producer)

Any idea how to solve this?

Navigator.push:

void _sendDataToSecondScreen(BuildContext context) {
    String textToSend = textFieldController.text;
    Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => Test2(producer: textToSend,),
        ));
  }
}

Test2.dart:

import 'package:flutter/material.dart';

class Test2 extends StatefulWidget {
  final String producer;
  List<String> Producerlist = [];

  Test2({Key key, @required this.producer}) : super(key: key);

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

class _Test2State extends State<Test2> {
  List<String> Producerlist = [];
  Producerlist.add (producer)

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Test2')),
        body: ListView.builder(
          itemCount: Producerlist.length,
          itemBuilder: (context, index){
            return Card(
              child: ListTile(
                onTap: (){},
                title: Text(Producerlist[index]),
              ),
            );
          },
        ),
    );
  }
}

Upvotes: 0

Views: 747

Answers (1)

w461
w461

Reputation: 2698

Either you pass producer to Test2State or (I believe, haven't tried myself but seen something like this) you use Producerlist.add (widget.producer)

UPDATE:

As I said, I haven't tried this with widget. myself, yet. So, to be on the sure side, try

import 'package:flutter/material.dart';

class Test2 extends StatefulWidget {
  final String producer;

  Test2({Key key, @required this.producer}) : super(key: key);

  @override
  _Test2State createState() => _Test2State(producer);
}

class _Test2State extends State<Test2> {
  _Test2State(this.producer);
  final String producer;
  List<String> Producerlist = [];
  Producerlist.add (producer)

Upvotes: 1

Related Questions