NoSuchMethodError(NoSuchMethodError:方法“ add”在null上调用。接收方:null尝试调用:add(“ CategoresList”的实例)

时间:2020-11-12 20:00:07

标签: flutter dart

我试图将数据库(MYSQL数据库)中的数据添加为json数据。我收到了此错误,因此我尝试在控制台中打印来自(MYSQL)的数据,并且该数据出现了,并且其中没有错误,但是当我尝试将其添加到下拉列表时,就会出现此错误。谁能帮我

 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,
          ),
        ],
      ),
    );
  }


}

这是类CategoresList

import 'package:flutter/material.dart';

class CategoresList {
  final name;

  CategoresList({this.name});
}

1 个答案:

答案 0 :(得分:0)

您正在尝试将值添加为null。首先,您需要初始化statesList。

 List statesList = new List();