Home > other >  type 'Null' is not a subtype of type 'TextEditingController' of 'function r
type 'Null' is not a subtype of type 'TextEditingController' of 'function r

Time:02-02

Im getting this error>>>>>

type 'Null' is not a subtype of type 'TextEditingController' of 'function result

lib/screens/login/edit_number.dart:48:45: Warning: Operand of null-aware operation '!' has type 'Map<String, dynamic>' which excludes null

  • 'Map' is from 'dart:core'. if(dataResult!= null) data =dataResult!;

how to solve this. here I have include my code.

edit_number.dart

import 'package:cupertino_list_tile/cupertino_list_tile.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:new_chat_app/components/logo.dart';
import 'package:new_chat_app/screens/login/select_country.dart';

class EditNumber extends StatefulWidget {
  const EditNumber({Key? key}) : super(key: key);

  @override
  _EditNumberState createState() => _EditNumberState();
}

class _EditNumberState extends State<EditNumber> {

  var _enterPhoneNumber = TextEditingController();
  Map<String, dynamic> data ={ "name": "Srilanka", "code": " 94"};
  late Map<String, dynamic> dataResult;

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text("EditNumber"),
        previousPageTitle: "Black",
      ),
      child: Column (
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Row(
            children: [Logo (width: 80.0, height:80.0, radius:30.0),
            Text("Verification one Step",
            style: TextStyle(
              color: Color(0xff08c187).withOpacity(0.7),fontSize: 30
            ),)],
          ),
          Text("Enter your phone number ",
            style: TextStyle(
                color: CupertinoColors.systemGrey.withOpacity(0.7),fontSize: 30
            ),),
          CupertinoListTile(
            onTap: ()async {
              dataResult =await Navigator.push(context,
                  CupertinoPageRoute(builder:(context)=> SelectCountry()));

              setState((){
                if(dataResult!= null) data =dataResult!;
              });
            },


            title: Text(data ['name'],style: TextStyle(color: Color(0xff08c187)),),),
          Padding(
            padding: const EdgeInsets.all(10.0),
            child: Row(
              children: [
                Text(data['code'],
                style: TextStyle(
                  fontSize: 25, color: CupertinoColors.secondaryLabel
                ),),
                Expanded(
                  child: CupertinoTextField(
                    placeholder:"Enter your phone number",
                    controller: _enterPhoneNumber,
                    keyboardType: TextInputType.number,
                      style: TextStyle(
                        fontSize: 25, color: CupertinoColors.secondaryLabel
                      ),
                  ),
                )
              ],
            ),
          ),
          Text("You will recieve an activation code in ahort time",
                  style: TextStyle(
                    color: CupertinoColors.systemGrey.withOpacity(0.7),fontSize: 15
              ),),
          Padding(
            padding: const EdgeInsets.symmetric(vertical:40),
            child: CupertinoButton.filled(child:Text("Request code"), onPressed:(){}),
          )


        ],
      ),

    );
  }
}

CodePudding user response:

you have defined dataResult as non-nullable. so null check doesn't make sense. so change late Map<String, dynamic> dataResult; to Map<String, dynamic>? dataResult;

CodePudding user response:

select_country.json
import 'dart:convert';
import 'package:cupertino_list_tile/cupertino_list_tile.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/cupertino/nav_bar.dart';

class SelectCountry extends StatefulWidget {
  const SelectCountry({Key? key}) : super(key: key);

  @override
  _SelectCountryState createState() => _SelectCountryState();
}

class _SelectCountryState extends State<SelectCountry> {
  List<dynamic>? dataRetrieved; // data decoded from the json file
  List<dynamic>? data; // data to display on the screen
  var _searchController = TextEditingController();
  var searchValue = "";

  @override
  void initState() {
    _getData();
  }

  Future _getData() async {
    final String response =
    await rootBundle.loadString('assets/CountryCodes.json');
    dataRetrieved = await json.decode(response) as List<dynamic>;
    setState(() {
      data = dataRetrieved;
    });
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      child: CustomScrollView(
        slivers: [
          CupertinoSliverNavigationBar(
            largeTitle: Text("Select Country"),
            previousPageTitle: "Edit Number",
          ),
          SliverToBoxAdapter(
            child: CupertinoSearchTextField(
              onChanged: (value) {
                setState(() {
                  searchValue = value;
                });
              },
              controller: _searchController,
            ),
          ),
          SliverList(
            delegate: SliverChildListDelegate((data != null)
                ? data!
                .where((e) => e['name']
                .toString()
                .toLowerCase()
                .contains(searchValue.toLowerCase()))
                .map((e) => CupertinoListTile(
              onTap: () {
                print(e['name']);
                Navigator.pop(context,
                    {"name": e['name'], "code": e['dial_code']});
              },
              title: Text(e['name']),
              trailing: Text(e['dial_code']),
            ))
                .toList()
                : [Center(child: Text("Loading"))]),
          )
        ],
      ),
    );
  }
}
  •  Tags:  
  • Related