【问题标题】:Tried calling: []("number")尝试调用:[]("number")
【发布时间】:2021-12-01 13:35:56
【问题描述】:

我正在尝试从实时数据库中获取一个值并将其与用户提供的输入相匹配。如果匹配,则导航到下一页,否则抛出错误,但在我正在实现从实时获取价值的方法的部分,我收到以下错误:

E/flutter (17592): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常: NoSuchMethodError: 方法 '[]' 在 null 上调用。 E/颤振(17592):接收器:空 E/flutter (17592):尝试调用:

import 'package:country_code_picker/country_code_picker.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'OTPController.dart';

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

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

class _LoginScreenState extends State<LoginScreen> {
  DataSnapshot snapshot;

  Query _ref;
  String dialCodeDigits = "+977";
  TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(height: 100),
            Padding(
              padding: const EdgeInsets.only(left: 28.0, right: 28.0),
              child: Image.asset("assets/prologo.png"),
            ),
            Container(
              margin: EdgeInsets.only(top: 10),
              child: Center(
                  child: Text(
                "Phone (OTP) Authentication",
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
              )),
            ),
            SizedBox(
              height: 50,
            ),
            SizedBox(
              height: 60,
              width: 400,
              child: CountryCodePicker(
                onChanged: (country) {
                  setState(() {
                    dialCodeDigits = country.dialCode;
                  });
                },
                initialSelection: "NPL",
                showCountryOnly: false,
                showOnlyCountryWhenClosed: false,
                favorite: ["+91", "IND", "+1", "USA"],
              ),
            ),
            Container(
                margin: EdgeInsets.only(top: 10, right: 10, left: 10),
                child: TextField(
                  decoration: InputDecoration(
                      hintText: "Phone Number",
                      prefix: Padding(
                        padding: EdgeInsets.all(4),
                        child: Text(dialCodeDigits),
                      )),
                  maxLength: 12,
                  keyboardType: TextInputType.number,
                  controller: _controller,
                )),
            Container(
              margin: EdgeInsets.all(15),
              width: double.infinity,
              child: ElevatedButton(
                onPressed: () async {
                  var numberPhone = dialCodeDigits + _controller.text;

                  FirebaseDatabase.instance
                      .reference()
                      .child('Subscribers')
                      .child('number')
                      .equalTo(numberPhone)
                      .once()
                      .then((DataSnapshot snapshot) {
                    Map numPhone = snapshot.value;
                    var phoneNumber = numPhone['number'];
                    print(phoneNumber);
                  });

                  if ('+917340868077' == numberPhone) {
                    Navigator.of(context).push(MaterialPageRoute(
                        builder: (c) => OTPController(
                            phoneNumber: _controller.text,
                            codeDigit: dialCodeDigits)));
                  }
                },
                child: Text(
                  'Next',
                  style: TextStyle(
                      color: Colors.white, fontWeight: FontWeight.bold),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: android flutter dart firebase-realtime-database


    【解决方案1】:

    您的错误很可能出现在这一行:
    var phoneNumber = numPhone['number'];

    由于numPhone 可以为空,您可以像这样进行检查:

    //... other lines
    .then((DataSnapshot snapshot) {
      if (snapshot.value == null) {
        // do sth with the null return
        print('no data');
        return;
      }
      Map numPhone = snapshot.value;
      var phoneNumber = numPhone['number'];
      print(phoneNumber);
    });
    

    【讨论】:

    • 感谢您的回复,但我认为我确实将值存储在实时数据库中,但我没有得到任何值。根据您提供的实施,我得到“没有数据”
    • @ashishrana 是的,我上面的代码只是针对最初询问的错误。由于不查看您的数据库有点模糊,我认为您应该仔细检查 .equalTo(numberPhone) 并查看 numberPhone 的格式是否正确并完全等于存储在 Firebase 中的值
    猜你喜欢
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 2021-07-11
    • 2017-03-06
    • 1970-01-01
    • 2011-07-30
    • 2020-08-27
    相关资源
    最近更新 更多