0

I am trying to add data from the database (MYSQL database) as json data. I received this error, so I tried to print the data that came from (MYSQL) in the console and it appeared and it did not contain an error, but when I try to add it to the drop-down list, this error appears. Can anyone Help me out

 import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:youtubeclone/pages/class/categores.dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  //CALLING STATE API HERE
// Get State information by API
  List statesList;
  String _myState;

  Future<String> _getStateList() async {
    String url = 'http://10.0.2.2/videoTube/chosecategories.php';
    var response = await http.get(url);
    var responsebody = json.decode(response.body);
    //print(data);
    setState(() {
      if (responsebody != null) {
        for (int i = 0; i < responsebody.length; i++) {
          print(responsebody[i]['name']);
          var name = responsebody[i]['name'];
          var cateygory_list = CategoresList(name: name);
          statesList.add(cateygory_list);
        }
      }
    });
  }

  // Get State information by API

  @override
  void initState() {
    _getStateList();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Dynamic DropDownList REST API'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Container(
            alignment: Alignment.topCenter,
            margin: EdgeInsets.only(bottom: 100, top: 100),
            child: Text(
              'KDTechs',
              style: TextStyle(fontWeight: FontWeight.w800, fontSize: 20),
            ),
          ),
          //======================================================== State

          Container(
            padding: EdgeInsets.only(left: 15, right: 15, top: 5),
            color: Colors.white,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Expanded(
                  child: DropdownButtonHideUnderline(
                    child: ButtonTheme(
                      alignedDropdown: true,
                      child: DropdownButton<String>(
                        value: _myState,
                        iconSize: 30,
                        icon: (null),
                        style: TextStyle(
                          color: Colors.black54,
                          fontSize: 16,
                        ),
                        hint: Text('Select State'),
                        onChanged: (String newValue) {
                          setState(() {
                            _myState = newValue;
                            //_getCitiesList();
                            print(_myState);
                          });
                        },
                        items: statesList?.map((item) {
                              return new DropdownMenuItem(
                                child: new Text(item['name']),
                                value: item['id'].toString(),
                              );
                            })?.toList() ??
                            [],
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          SizedBox(
            height: 30,
          ),
        ],
      ),
    );
  }


}

this is class CategoresList

import 'package:flutter/material.dart';

class CategoresList {
  final name;

  CategoresList({this.name});
}
Muhammed Jack
  • 77
  • 2
  • 9
  • 2
    Does this answer your question? [What is a NoSuchMethod error and how do I fix it?](https://stackoverflow.com/questions/64049102/what-is-a-nosuchmethod-error-and-how-do-i-fix-it) – nvoigt Nov 12 '20 at 20:03

1 Answers1

0

You are trying to add value to null. First, you need to initialize your statesList.

 List statesList = new List();
Akif
  • 7,098
  • 7
  • 27
  • 53
  • i got this error after i fix it (--------Class 'CategoresList' has no instance method '[]'. Receiver: Instance of 'Categories List' Tried calling: []("name")---------) – Muhammed Jack Nov 12 '20 at 20:36