龍が如く
龍が如く

Reputation: 309

Flutter add value to another class list

I want to add the date picker value to the List.

When user after select the date, I will calling function [addToDateList] in _AddFieldDynamicItem.

For this function, it will add to list:

List <DateTime> dateList=[];

Which is in _AddFieldDynamicTest

After click "Add another", I don't know why that it only can record the latest picker value.

Did I need to using another storage method such as sql lite or shared preferences?

import 'dart:convert';

import 'package:flutter/material.dart';
class AddFieldDynamicTest extends StatefulWidget {
  @override
  _AddFieldDynamicTest createState() => _AddFieldDynamicTest();
}

class _AddFieldDynamicTest extends State<AddFieldDynamicTest> {
  Map<String, String> _formdata = {};

  var _myPets = List<Widget>();
  
  List <DateTime> dateList=[];

  int _index = 1;

  void _add() {
    _myPets = List.from(_myPets)
      ..add(AddFieldDynamicItem(_index));

    setState(() {
      _index += 1;
    });
  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _add();
  }

  addToDateList(DateTime d){
      dateList.add(d);  
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () => getDateList(),
        child: Text('Save'),
      ),
      appBar: AppBar(
        title: Text('Add Test 2'),
        actions: <Widget>[
          FlatButton(
            child: Text('Add another'),
            onPressed: _add,
          ),
        ],
      ),
      body: ListView(
        children: _myPets,
      ),
    );
  }
  getDateList(){
    print(dateList.length);
  }
}

class AddFieldDynamicItem extends StatefulWidget {
  AddFieldDynamicItem(this._index);
  final int _index;
  @override
  _AddFieldDynamicItem createState() => _AddFieldDynamicItem(_index);
}

class _AddFieldDynamicItem extends State<AddFieldDynamicItem> {

  _AddFieldDynamicItem(this._index);
  String _value = '';
  final int _index;
  List<DateTime> d=[];

  Future _selectDate() async {
    DateTime picked = await showDatePicker(
        context: context,
        initialDate: new DateTime.now(),
        firstDate: new DateTime(2000),
        lastDate: new DateTime(2100)
    );
    if(picked != null)
      _AddFieldDynamicTest().addToDateList(picked);
      setState(() {
        _value = picked.toString();
      });
  }
  
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        new Column(
          children: <Widget>[
            new Text('$_index . Current Date'),
            new Text(_value),
            new RaisedButton(onPressed: _selectDate, child: new Text('Date picker'),)
          ],
        )
      ],
    );
  }
}

Upvotes: 2

Views: 706

Answers (1)

Yuu Woods
Yuu Woods

Reputation: 1348

"_AddFieldDynamicTest().addToDateList(picked);" is make new instance.

So, fix like this.

from

_myPets = List.from(_myPets)
  ..add(AddFieldDynamicItem(_index));

to

_myPets = List.from(_myPets)
  ..add(AddFieldDynamicItem(_index, addToDateList));

from

AddFieldDynamicItem(this._index);
final int _index;

to

AddFieldDynamicItem(this._index, this.addToDateList);
final int _index;
final Function(DateTime) addToDateList;

from

if(picked != null)
  _AddFieldDynamicTest().addToDateList(picked);

to

if (picked != null) widget.addToDateList(picked);

Upvotes: 1

Related Questions