0

I'm stuck with a DrowdownButton that is not populating any data.

It's populating from a list passed from the previous screen.

My debug indicates that the list has data that can be parsed. The output just produces an empty DropdownButton.

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'menuList.dart';
import 'package:flutter_calendar_carousel/flutter_calendar_carousel.dart' show CalendarCarousel;

class OrderInitiate extends StatefulWidget {
  final List childData;

  OrderInitiate(this.childData);

  @override
  _OrderInitiateState createState() => new _OrderInitiateState();
}

class _OrderInitiateState extends State<OrderInitiate> {

  double buttonWidth = 200.0;
  DateTime selectedDate = DateTime.now();
  DateTime tempDate = DateTime.now();

  @override
  void initState() {
    print("OrderInitiate: initstate begin");
    print("ChildData: " + widget.childData.toString());
  }

  _dateSelected(date) {
    setState(() {
      selectedDate = date;
    });
  }


  String itemSel;

  @override
  Widget build(BuildContext context) => new Scaffold(

        appBar: new AppBar(title: new Text("Start an Order"), actions: 
        <Widget>[
        ]),
        body: new Column(
          children: <Widget>[
            Center(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Text("Select child:"),
                  new DropdownButton<String>(
                    value: itemSel,
                    onChanged: null,
                    hint: Text("Select child"),
                    items:
                      widget.childData.map ((item) {
                      print("DropDownButton item: " + item.toString());
                      print("Child Id: " + item['ChildId']);
                      return new DropdownMenuItem<String>(
                          value: item['ChildId'],
                          child:
                          new Text("ChildFirstName"),
                      );
                    }).toList(),
                  ),

                  Text("${selectedDate.toLocal()}"),
                  SizedBox(
                    height: 20.0,
                  ),
                ],
              ),
            ),
            SizedBox(
              width: buttonWidth,
              child: RaisedButton(
                onPressed: () => _StartOrder(this.context),
                child: Text('Start Order'),
              ),
            ),
          ],
        ),
      );

  _StartOrder(BuildContext context) {
    Navigator.push(
        context,
        new MaterialPageRoute(
            builder: (BuildContext context) => new MenuList()));
  }
}

My debug lines give the following output:

DropDownButton item: {ParentId: 4, ParentLastName: testparent, ParentFirstName: TestParentSur, ParentUserName: TestParent, ChildId: 3, ChildLastName: Test2Last, ChildFirstName: Test2, ChildUserName: test2}

Child Id: 3

I've been stuck on this code for the last 2 nights, so hopefully someone can help.

I've studied numerous List and Item generation tutorials and stackoverflow items with no luck.

Community
  • 1
  • 1
Duncan Pullen
  • 355
  • 3
  • 13

1 Answers1

0

Try changing the onChanged value of your DropDownButton to:

onChanged: (int i) {
  setState(() => itemSel = i);
},

If onChanged == null then the field is essentially disabled, so while your data might be there it won't present anything when you tap it. The selected value of the field also doesn't automatically update, so setting state with the new selected value is important too.

greyaurora
  • 837
  • 7
  • 8