未定义涉及操作员的颤振错误

时间:2020-09-10 01:06:26

标签: flutter dart

我是新手,我正在学习本教程,但目前遇到此错误

error: The operator '[]' isn't defined for the type 'Map<String, dynamic> Function()'. (undefined_operator at [chat_app_tutorial] lib\views\search.dart:33)

请问我已经有超过2天的错误了。我在这里检查过,并且一直在网上进行调查,但是到目前为止,我还没有发现任何有效的方法。这是我从中得到错误的代码行。 我不确定要帮助我需要代码的哪一部分,所以我将把所有内容放进去。

import 'package:chat_app_tutorial/widgets/widget.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class SearchScreen extends StatefulWidget {
  @override
  _SearchScreenState createState() => _SearchScreenState();
}

class _SearchScreenState extends State<SearchScreen> {
  
  DatabaseMethods databaseMethods = new DatabaseMethods();
  TextEditingController searchTextEditingController = new TextEditingController();

  QuerySnapshot searchSnapshot;

  initiateSearch(){
    databaseMethods.getUserByUsername(searchTextEditingController.text).then((val){
      setState(() {
        searchSnapshot = val;
      });
    });
  }

  Widget searchList(){
    return searchSnapshot != null ? ListView.builder(
        itemCount: searchSnapshot.docs.length,
        shrinkWrap: true,
        itemBuilder: (context, index){
          return SearchTile(

            userName: searchSnapshot.docs[0].data["name"],
            userEmail: searchSnapshot.docs[0].data["email"],
          );
        }
    ) : Container();
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBarMain(context),
      body: SingleChildScrollView(
        child: Container(
          child: Column(
            children: [
              Container(
                color: Color(0x54ffffff),
                padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
                child: Row(
                  children: [
                    Expanded(
                        child: TextField(
                          controller: searchTextEditingController,
                          style: TextStyle(
                            color: Colors.white
                          ),
                          decoration: InputDecoration(
                            hintText: "Search username",
                            hintStyle: TextStyle(
                              color: Colors.white54
                            ),
                            border: InputBorder.none
                          ),
                        ),
                    ),
                    GestureDetector(
                      onTap: (){
                        initiateSearch();
                      },
                      child: Container(
                          height: 40,
                          width: 40,
                          decoration: BoxDecoration(
                            gradient: LinearGradient(
                              colors: [
                                const Color(0x36ffffff),
                                const Color(0x0fffffff)
                              ]
                            ),
                            borderRadius: BorderRadius.circular(40)
                          ),
                          padding: EdgeInsets.all(12),
                          child: Image.asset("assets/images/search_white.png")
                      ),
                    )
                  ],
                ),
              ),
              searchList()
            ],
          ),
        ),
      ),
    );
  }
}

class SearchTile extends StatelessWidget {
  final String userName;
  final String userEmail;
  SearchTile({this.userName, this.userEmail});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        children: [
          Column(
            children: [
              Text(userName, style: simpleTextStyle(),),
              Text(userEmail, style: simpleTextStyle(),)
            ],
          ),
          Spacer(),
          Container(
            decoration: BoxDecoration(
              color: Colors.blue,
              borderRadius: BorderRadius.circular(30)
            ),
            padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            child: Text("Message"),
          )
        ],
      ),
    );
  }
}```

1 个答案:

答案 0 :(得分:0)

该错误抱怨:

 return SearchTile(

  userName: searchSnapshot.docs[0].data["name"],
  userEmail: searchSnapshot.docs[0].data["email"],
);

searchSnapshot.docs[0].data函数,它返回Map。您需要先调用该功能。

您可能想要searchSnapshot.docs[0].data()['name']searchSnapshot.docs[0].data()['email']

更好的是避免多次调用data()

var data = searchSnapshot.docs[0].data();
return SearchTile(userName: data['name'], userEmail: data['email']);