简体   繁体   English

颤振未处理的异常:NoSuchMethodError:方法'then'在null上被调用

[英]Flutter Unhandled Exception: NoSuchMethodError: The method 'then' was called on null

Exception has occurred.发生异常。 NoSuchMethodError (NoSuchMethodError: The method 'then' was called on null. Receiver: null Tried calling: then(Closure: (dynamic) => Null)) Friends, I am getting the error I mentioned above. NoSuchMethodError (NoSuchMethodError: The method 'then' was called on null. Receiver: null 尝试调用: then(Closure: (dynamic) => Null))朋友们,我收到了上面提到的错误。 Does anyone know how to solve this?有谁知道如何解决这个问题? I'm trying to user search via firebase.Does anyone know any other way to do this?我正在尝试通过firebase进行用户搜索。有人知道其他方法吗?

    class _DiscoverScreen extends State<DiscoverScreen> {
  DatabaseMethods databaseMethods = DatabaseMethods();
  TextEditingController searchEditingController = TextEditingController();
  QuerySnapshot? searchResultSnapshot;

  @override
  void dispose() {
    searchEditingController.dispose();
    super.dispose();
  }

  initiateSearch() async {
    if (searchEditingController.text.isNotEmpty) {
      await databaseMethods
          .searchByName(searchEditingController.text)
          .then((value) {
        value.docs.forEach(
          (result) {
            searchResultSnapshot = value;
            print("$searchResultSnapshot");
          },
        );
      });
    }
  }

  Widget userList() {
    return searchResultSnapshot != null
        ? ListView.builder(
            shrinkWrap: true,
            itemCount: searchResultSnapshot!.docs.length,
            itemBuilder: (context, index) {
              return SearchTile(
                name: searchResultSnapshot!.docs[index].get("name"),
                username: searchResultSnapshot!.docs[index].get("username"),
                image: searchResultSnapshot!.docs[index].get("photoUrl"),
              );
            })
        : Container();
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Scaffold(
      appBar: AppBar(
        centerTitle: false,
        title: Text("Keşfet"),
        backgroundColor: Colors.grey.shade700,
      ),
      body: Column(
        children: [
          Container(
            padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
            child: Row(
              children: [
                Expanded(
                  child: TextField(
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: "Search username",
                      hintStyle: TextStyle(
                        color: Colors.black,
                        fontSize: 16,
                      ),
                    ),
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 20,
                    ),
                    controller: searchEditingController,
                  ),
                ),
                IconButton(
                    onPressed: () {
                      initiateSearch();
                    },
                    icon: ImageIcon(
                      AssetImage(
                        "assets/icons/searchicon.png",
                      ),
                      size: 20,
                    ))
              ],
            ),
          ),
          userList(),
        ],
      ),
    );
  }
}

searchByName Codes = searchByName 代码 =

searchByName(String username) {
    FirebaseFirestore.instance
        .collection("users")
        .where("username", isEqualTo: username)
        .get();
  }

It seems like searchByName is returning null.似乎searchByName正在返回 null。 Either do a null check using the ?要么使用? operator or provide a code sample with the code containing searchByName function in order to find the problem.运算符或提供包含searchByName函数的代码示例以查找问题。

we use method then with future mostly我们主要使用未来的方法

make this databaseMethods Future type使这个数据库方法未来类型

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的异常 NoSuchMethodError:方法 &#39;[]&#39; 在 Flutter 中被调用为 null - Unhandled Exception NoSuchMethodError: The method '[]' was called on null in flutter [Flutter] 未处理的异常:NoSuchMethodError:方法 &#39;[]&#39; 在 null 上被调用 - [Flutter ]Unhandled Exception: NoSuchMethodError: The method '[]' was called on null 未处理的异常:NoSuchMethodError:在 flutter 上的 null 上调用了方法“[]” - Unhandled Exception: NoSuchMethodError: The method '[]' was called on null on flutter Flutter - 未处理的异常:NoSuchMethodError:在 null 上调用了方法“validate” - Flutter - Unhandled Exception: NoSuchMethodError: The method 'validate' was called on null Flutter 错误:未处理的异常:NoSuchMethodError:在 null 上调用了方法“findAncestorStateOfType” - Flutter Error: Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null 未处理的异常:NoSuchMethodError:在 null 上调用了方法“插入”。 Flutter - Unhandled Exception: NoSuchMethodError: The method 'insert' was called on null. Flutter 未处理的异常:NoSuchMethodError:在 null 上调用了方法“addAdvogado”。 关于颤振 - Unhandled Exception: NoSuchMethodError: The method 'addAdvogado' was called on null. On Flutter 如何修复未处理的异常:NoSuchMethodError:在 null flutter firestore 上调用了方法“[]” - How to fix Unhandled Exception: NoSuchMethodError: The method '[]' was called on null flutter firestore 未处理的异常:NoSuchMethodError:在 null 上调用了方法“关闭”。 Flutter BLoC - Unhandled Exception: NoSuchMethodError: The method 'close' was called on null. Flutter BLoC Flutter:未处理的异常:NoSuchMethodError:在 null 上调用了方法“findAncestorStateOfType” - Flutter: Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM