错误:空值检查运算符用于空值或列表<动态>不是 Map<String, dynamic> 类型的子类型 [英] error: Null check operator used on a null value OR List<dynamic> is not a subtype of type Map<String, dynamic>

查看:18
本文介绍了错误:空值检查运算符用于空值或列表<动态>不是 Map<String, dynamic> 类型的子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取两个具有相同 uniq id 的不同页面 api我希望button的API的user_uniq_id和team_list的API的user_uniq_id匹配,匹配后显示其用户的数据.

fetch Two different pages api with same uniq id I want the user_uniq_id of the API of the button and the user_uniq_id of the API of the team_list to match and show the data of its user after the match.

此代码用于第一个文件此代码是来自 api 的按钮

this code is for first File This code is of the button that is coming from the api

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:practice/left_team.dart';
import 'package:http/http.dart' as http;

class Button extends StatefulWidget {
  @override
  _ButtonState createState() => _ButtonState();
}

class _ButtonState extends State<Button> {
  var api = Uri.parse('http://192.***.***.***/flutter/teamApi.php');
  var response;
  var teamApi;

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

  fetchData() async {
    response = await http.get(api);
    print(response.body);
    teamApi = jsonDecode(response.body);

    setState(() {});
  }
  @override
  Widget build(BuildContext context) {
    var color = 0xff453658;
    return Scaffold(

      appBar: AppBar(
        backgroundColor: Color(0xff392850),
        title: Row(
          children: [

            InkWell(
                onTap: () {
                  Navigator.pop(context);
                },
                child: Icon(Icons.arrow_back)),

            SizedBox(
              width: 10.0,
            ),
            Text(
              "Income",
              style: TextStyle(fontStyle: FontStyle.italic),
            ),

          ],
        ),
        actions: [
          IconButton(
            icon: Icon(Icons.person),

            onPressed: () => print("open cart"),
          ),
        ],
      ),

      body: Column(
        children: [
          Container(
            margin: const EdgeInsets.all(20.0),

            padding: const EdgeInsets.all(10.0),
            decoration: BoxDecoration(
                border: Border.all(width: 2, color: Color(color))),
            child: Row(

              children: [
                Text(
                  "Total Income:",
                  style: TextStyle(

                      fontSize: 30.0,
                      fontWeight: FontWeight.bold,
                      fontStyle: FontStyle.italic),
                ),

                SizedBox(
                  width: 10.0,
                ),
                Text(

                  "Rs.2000",
                  style: TextStyle(
                      fontSize: 20.0,
                      fontWeight: FontWeight.bold,

                      fontStyle: FontStyle.italic),
                ),
              ],
            ),

          ),
          SizedBox(
            height: 20.0,
          ),

          Flexible(
            child: Container(
              child: GridView.count(
                childAspectRatio: 1.0,

                padding: EdgeInsets.only(left: 16, right: 16),
                crossAxisCount: 3,
                crossAxisSpacing: 18,
                mainAxisSpacing: 18,

                children: List.generate(
                  teamApi.length,
                  (index) => GestureDetector(
                    onTap: () {

                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) =>

                                LeftTeam(teamData: teamApi[index])),
                      );
                    },
                    child: Container(

                      decoration: BoxDecoration(
                          color: Color(0xff00ffff),
                          borderRadius: BorderRadius.circular(10)),
                      child: Column(

                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Center(
                            child: Text(teamApi[index]["teamType"],

                                style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 16,
                                    fontWeight: FontWeight.w600)),

                          ),
                        ],
                      ),
                    ),

                  ),
                ),
              ),
            ),

          ),
        ],
      ),
    );

  }
}

这是按钮的API数据

[{"teamType":"direct team","team_name":"platinum","team_number":"234","team_uniq_id":"1","team_last_update":"10-may-2021"},{"teamType":"left team","team_name":"gold","team_number":"356","team_uniq_id":"2","team_last_update":"10-may-2021"},{"teamType":"right team","team_name":"silver","team_number":"876","team_uniq_id":"3","team_last_update":"10-may-2021"}]

这是第二个文件的代码.这是模型部分.

this is code for second file. this is model part.

class MyData {
  List<Results> user = [];

  MyData.fromJson(Map<String, dynamic> json) {
    // previous = json['previous'];
    // next = json['next'];
    if (json['results'] != null) {
      user = <Results>[];
      json['results'].forEach((v) {
        user.add(new Results.fromJson(v));
      });
    }
  }
}

class Results {
  String user_name = "";
  String user_mother_name = "";
  String user_address = "";
  String user_mobile = "";
  String user_sponsor_id = "";
  String sponsor_id = "";
  String email = "";
  String city = "";
  String state = "";
  String dob = "";

  Results.fromJson(Map<String, dynamic> json) {
    user_name = json['user_name'];
    user_mother_name = json['user_mother_name'];
    user_address = json['user_address'];
    user_mobile = json['user_mobile'];
    user_sponsor_id = json['user_sponsor_id'];
    email = json['email'];
    city = json['city'];
    state = json['state'];
    dob = json['dob'];
  }
}

这个 api 链接的提供者.

this provider of api link.

import 'dart:convert';
import 'package:flutter/widgets.dart';
import 'package:http/http.dart' as http;
import 'package:practice/LeftTeamFile/model/myData.dart';

class MyHomePageProvider extends ChangeNotifier {
  MyData? data;
  Future getData(context) async {
    
    var url = Uri.parse(
  var url = Uri.parse('http://192.***.***.***/flutter/team_list.php');
    var response = await http.get(url);
    print("res${response.body}");

    var mJson = json.decode(response.body);
    this.data = MyData.fromJson(mJson);
    this.notifyListeners(); 

这是 teamList 部分.

this is teamList part .

import 'package:flutter/material.dart';
import 'package:practice/LeftTeamFile/provider/myHomePageProvider.dart';
import 'package:provider/provider.dart';

class TeamList extends StatefulWidget {
  final teamData;

  const TeamList({Key? key, this.teamData}) : super(key: key);
  @override
  _TeamListState createState() => _TeamListState();
}

class _TeamListState extends State<TeamList> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [

          Container(
            padding: EdgeInsets.all(10.0),
            height: 100.0,
            color: Color(0xffedbf6b),
            child: Row(

                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      Icon(

                        Icons.list_alt,
                        color: Colors.white,
                      ),
                      Text(
                        widget.teamData['team_uniq_id'],

                        style: TextStyle(
                          fontSize: 20.0,
                          color: Colors.white,
                        ),
                      ),

                    ],
                  ),
                ]),
          ),
          SizedBox(

            height: 10.0,
          ),
          Stack(
            children: [
              ChangeNotifierProvider<MyHomePageProvider>(

                create: (context) => MyHomePageProvider(),
                child: Consumer<MyHomePageProvider>(
                  builder: (context, provider, child) {
                   if (provider.data!.team_uniq_id ==
                        widget.teamData['team_uniq_id']) {
                      print("prov $provider.data");

                      return SingleChildScrollView(
                        scrollDirection: Axis.horizontal,
                        // Data table widget in not scrollable so we have to wrap it in a scroll view when we have a large data set..
                        child: SingleChildScrollView(
                          child: DataTable(
                    
                          columns: [
                             DataColumn(
                             label: Text('Name'),
                             tooltip: 'represents if user is verified.'),
                            DataColumn(

                                label: Text('Mother_name'),
                                tooltip: 'represents first S no of the user'),
                            DataColumn(
                                label: Text('address'),
                                tooltip: 'represents Sponsor ID of the user'),

                            DataColumn(
                                label: Text('mobile'),
                                tooltip: 'represents User ID of the user'),
                            DataColumn(
                                label: Text('User_s_id'),

                                tooltip: 'represents Name of the user'),
                            DataColumn(
                                label: Text('sponsor_id'),
                                tooltip: 'represents Mobile of the user'),
                            DataColumn(

                                label: Text('email'),
                                tooltip: 'represents Date of the user'),
                            DataColumn(
                                label: Text('city'),
                                tooltip: 'represents Date of the user'),

                            DataColumn(
                                label: Text('state'),
                                tooltip: 'represents Date of the user'),
                            DataColumn(
                                label: Text('dob'),
                                tooltip: 'represents Date of the user'),
                          
                          ],

                          rows: provider.data!.user
                              .map((data) =>
                                  // we return a DataRow every time
                                  DataRow(
                                      // List<DataCell> cells is required in every row

                                      cells: [
                                       red when unverified
                                        DataCell(Text(data.user_name)),
                                        DataCell(Text(data.user_mother_name)),
                                        DataCell(Text(data.user_address)),
                                        DataCell(Text(data.user_mobile)),

                                        DataCell(Text(data.user_sponsor_id)),
                                        DataCell(Text(data.sponsor_id)),
                                        DataCell(Text(data.email)),
                                        DataCell(Text(data.city)),

                                        DataCell(Text(data.state)),
                                        DataCell(Text(data.dob)),
                                      ]))
                              .toList(),
                        ),
                      ),
                    );

                  },  provider.getData(context);
                    return Center(child: CircularProgressIndicator());
                ),
              ),
            ],
          ),
        ],

      ),
    );
  }
}

这是团队列表 api

[
  {
    "teamType": "direct Team",
    "team_uniq_id": "1",
    "user": [
      {
        "user_name": "deepak",
        "user_mother_name": "Accomodation",
        "user_address": "varanasi",
        "user_mobile": "5678989",
        "user_sponsor_id": "123456",
        "sponsor_id": "3456",
        "email": "abc@gmai.com",
        "city": "varanasi",
        "state": "India",
        "dob": "12-5-1996"
      },
      {
        "user_name": "deepak",
        "user_mother_name": "Accomodation",
        "user_address": "varanasi",
        "user_mobile": "5678989",
        "user_sponsor_id": "123456",
        "sponsor_id": "3456",
        "email": "abc@gmai.com",
        "city": "varanasi",
        "state": "India",
        "dob": "12-5-1996"
      },
      {
        "user_name": "deepak",
        "user_mother_name": "Accomodation",
        "user_address": "varanasi",
        "user_mobile": "5678989",
        "user_sponsor_id": "123456",
        "sponsor_id": "3456",
        "email": "abc@gmai.com",
        "city": "varanasi",
        "state": "India",
        "dob": "12-5-1996"
      }
    ]
  },
  {
    "teamType": "left Team",
    "team_uniq_id": "2",
    "user": [
      {
        "user_name": "Ashu",
        "user_mother_name": "manju",
        "user_address": "Mirzapur",
        "user_mobile": "222222",
        "user_sponsor_id": "123456",
        "sponsor_id": "3456",
        "email": "abc@gmai.com",
        "city": "varanasi",
        "state": "India",
        "dob": "12-5-1996"
      },
      {
        "user_name": "Ashutodh",
        "user_mother_name": "manju1",
        "user_address": "Mirzapur1",
        "user_mobile": "2222221",
        "user_sponsor_id": "1234561",
        "sponsor_id": "34561",
        "email": "abc@gmai.com1",
        "city": "varanasi1",
        "state": "India1",
        "dob": "12-5-19961"
      }
    ]
  },
  {
    "teamType": "Right Team",
    "team_uniq_id": "3",
    "user": [
      {
        "user_name": "tosh",
        "user_mother_name": "snju",
        "user_address": "Allahabad",
        "user_mobile": "44444444",
        "user_sponsor_id": "333456",
        "sponsor_id": "6666666",
        "email": "jkl@gmai.com",
        "city": "lucknow",
        "state": "India",
        "dob": "15-3-1956"
      }
    ]
  },
  {
    "teamType": "Total Team",
    "team_uniq_id": "4",
    "user": [
      {
        "user_name": "tosh",
        "user_mother_name": "snju",
        "user_address": "Allahabad",
        "user_mobile": "44444444",
        "user_sponsor_id": "333456",
        "sponsor_id": "6666666",
        "email": "jkl@gmai.com",
        "city": "lucknow",
        "state": "India",
        "dob": "15-3-1956"
      },
      {
        "user_name": "deepak",
        "user_mother_name": "Accomodation",
        "user_address": "varanasi",
        "user_mobile": "5678989",
        "user_sponsor_id": "123456",
        "sponsor_id": "3456",
        "email": "abc@gmai.com",
        "city": "varanasi",
        "state": "India",
        "dob": "12-5-1996"
      },
      {
        "user_name": "deepak",
        "user_mother_name": "Accomodation",
        "user_address": "varanasi",
        "user_mobile": "5678989",
        "user_sponsor_id": "123456",
        "sponsor_id": "3456",
        "email": "abc@gmai.com",
        "city": "varanasi",
        "state": "India",
        "dob": "12-5-1996"
      },
      {
        "user_name": "tosh",
        "user_mother_name": "snju",
        "user_address": "Allahabad",
        "user_mobile": "44444444",
        "user_sponsor_id": "333456",
        "sponsor_id": "6666666",
        "email": "jkl@gmai.com",
        "city": "lucknow",
        "state": "India",
        "dob": "15-3-1956"
      }
    ]
  }
]

我觉得出现了一些错误.

I feel some error is coming.

推荐答案

您可以尝试确保将从服务器获取的对象转换为列表,以便能够将列表传递到小部件中

You can try to make sure you convert the object you are getting from the server to list, to be able to pass in the list into the widget

这篇关于错误:空值检查运算符用于空值或列表&lt;动态&gt;不是 Map&lt;String, dynamic&gt; 类型的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆