【问题标题】:Error: Problem in switching bw Multiple Tabs. Flutter错误:切换 bw 多个选项卡时出现问题。扑
【发布时间】:2020-09-22 12:32:20
【问题描述】:

所以我正在尝试创建一个包含 3 个选项卡的个人资料屏幕...个人资料、最近的和审查但是在尝试这样做时我遇到了一个错误。我无法代表所有 3 个选项卡。最近的标签有这个小部件

  Widget RecentItems() {
   return Padding(
      padding: const EdgeInsets.all(10.0),
      child: StreamBuilder(
          stream: Firestore.instance
              .collection("users")
              .document(uid)
              .collection("recent")
              .snapshots(),
          builder: (context, snapshot) {
            print(snapshot.data);
            List orders = List.from(Map.from(snapshot.data.data)['orders']);
            Map order;
            for (int i = 0; i < orders.length; i++) {
              if (orders[i]['orderId'] == widget.map['orderId'] &&
                  orders[i]['homemaker'] == widget.map['homemaker']) {
                order = orders[i];
                break;
              }
            }
            if (snapshot.data.isEmpty) {
              return Center(
                  child:
                  Text("OOPS, Looks like no one is serving!"));
            }
            print(order);
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Center(child: CircularProgressIndicator());
            } else if (snapshot.hasData) {
              print(snapshot.data.documents[0].data);
              return Container(
                height: 400,
                child: ListView.builder(
                    itemCount: snapshot.data.documents.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Container(
                        margin: EdgeInsets.all(10.0),
                        width: MediaQuery
                            .of(context)
                            .size
                            .width,
                        height: 85,
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(10.0),),
                        child: Padding(
                          padding: const EdgeInsets.all(10.0),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Row(
                                children: <Widget>[
                                  Expanded(child: Text(
                                    "${snapshot.data.documents[index]
                                        .data["dishname"]}", style: TextStyle(
                                      fontSize: 15,
                                      fontWeight: FontWeight.bold),)),
                                  //Icon: how to access if food is veg or not
                                ],
                              ),
                              // SizedBox(height:5),
                              Row(
                                children: <Widget>[
                                  Expanded(child: Text(
                                    "${snapshot.data.documents[index]
                                        .data["homemaker"]}",
                                    style: TextStyle(fontSize: 10),)),
                                  Text("${snapshot.data.documents[index]
                                      .data["rating"]}",
                                      style: TextStyle(fontSize: 15)),
                                  Icon(
                                    Icons.star, color: Colors.yellow.shade800,
                                    size: 20,)
                                ],
                              ),
                              SizedBox(height: 5),
                              //How to access order date
                              Text(
                                "Ordered ${DateTime
                                    .parse(order['order_placed_at']
                                    .toDate()
                                    .toString())
                                    .day}/${DateTime
                                    .parse(order['order_placed_at']
                                    .toDate()
                                    .toString())
                                    .month}/${DateTime
                                    .parse(order['order_placed_at']
                                    .toDate()
                                    .toString())
                                    .year}}",
                                style: TextStyle(fontSize: 15.0,
                                    fontWeight: FontWeight.bold),
                              ),
                            ],
                          ),
                        ),
                      );
                    }),
              );
            } //
          }),
    );
  }

这就是我试图展示它们的方式......

int _selectedIndex = 3;

  var uid;

  Future<String> getUser() async {
    final FirebaseUser user = await _auth.currentUser().then((val) {
      setState(() {
        uid = val.uid;
      });
    });
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
    if (_selectedIndex == 0) {
      Navigator.pushReplacementNamed(context, "/ExplorePage");
    } else if (_selectedIndex == 1) {
      Navigator.pushReplacementNamed(context, "/SearchPage");
    } else if (_selectedIndex == 2) {
      Navigator.pushReplacementNamed(context, "/FavoriteScreenPage",
          arguments: uid);
    }
  }
  TabController tabController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: Padding(
          padding: EdgeInsets.fromLTRB(20, 0, 20, 20),
          child: SizedBox(
            height: 54,
            child: BottomNavigationBar(
              showSelectedLabels: false,
              showUnselectedLabels: false,
              backgroundColor: Color.fromRGBO(255, 255, 255, 0.8),
              currentIndex: _selectedIndex,
              selectedItemColor: Color(0xffFE506D),
              onTap: _onItemTapped,
              items: [
                BottomNavigationBarItem(
                  icon: Icon(
                    Icons.explore,
                    color: Colors.black,
                  ),
                  title: Text("Explore"),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.search, color: Colors.black),
                  title: Text("Search"),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.bookmark_border, color: Colors.black),
                  title: Text("Faavorites"),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.perm_identity, color: Color(0xffFE506D)),
                  title: Text("Shop"),
                )
              ],
            ),
          )),
      drawer: DrawerWidget(uid: this.uid),
      appBar: AppBar(
        backgroundColor: Colors.white,
        iconTheme: IconThemeData(color: Colors.black),
        elevation: 0,
      ),
      backgroundColor: Color(0xffE5E5E5),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            ClipRRect(
              borderRadius: BorderRadius.only(
                  bottomRight: Radius.circular(25),
                  bottomLeft: Radius.circular(25)),
              child: Container(
                height: 230,
                width: MediaQuery.of(context).size.width,
                decoration: new BoxDecoration(
                  // border: new Border.all(width: 1.0, color: Colors.black),
                  shape: BoxShape.rectangle,
                  color: Colors.white,
                  boxShadow: <BoxShadow>[
                    BoxShadow(
                      color: Colors.black,
                      offset: Offset(20.0, 30.0),
                      blurRadius: 40.0,
                    ),
                  ],
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Container(
                        width: 155,
                        height: 155,
                        decoration: new BoxDecoration(
                            shape: BoxShape.circle,
                            image: new DecorationImage(
                                fit: BoxFit.cover,
                                image: new NetworkImage("$_image")))),
                    SizedBox(height: 10),
                    Text("$_name",
                        style: TextStyle(
                            fontSize: 25, fontWeight: FontWeight.bold))
                  ],
                ),
              ),
            ),
            SizedBox(
              height: 20,
            ),
            Padding(
              padding: const EdgeInsets.only(left: 15.0),
              child: Container(
                // margin: EdgeInsets.only(left: 15.0),
                child: Container(
                // margin: EdgeInsets.only(left: 15.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    GestureDetector(
                      onTap: () {
                        setState(() {
                          menuType = "profile";
                        });
                      },
                      child: Container(
                          margin: EdgeInsets.all(5.0),
                          child: Text(
                            getTranslated(context, "profile"),
                            style: TextStyle(
                                fontSize: menuType == "profile" ? 20.0 : 17.0,
                                color: menuType == "profile"
                                    ? Colors.black
                                    : Colors.black45,
                                fontWeight: FontWeight.bold,
                                fontFamily: "Gilroy"),
                          )),
                    ),
                    GestureDetector(
                      onTap: () {
                        setState(() {
                          menuType = "reviews";
                        });
                      },
                      child: Container(
                          margin: EdgeInsets.all(5.0),
                          child: Text(
                            getTranslated(context, "reviews"),
                            style: TextStyle(
                                fontSize: menuType == "reviews" ? 20.0 : 17.0,
                                color: menuType == "reviews"
                                    ? Colors.black
                                    : Colors.black45,
                                fontWeight: FontWeight.bold,
                                fontFamily: "Gilroy"),
                          )),
                    ),
                    GestureDetector(
                      onTap: () {
                        setState(() {
                          menuType = "recent";
                        });
                      },
                      child: Container(
                          margin: EdgeInsets.all(5.0),
                          child: Text(
                            getTranslated(context, "recent"),
                            style: TextStyle(
                                fontSize: menuType == "recent" ? 20.0 : 17.0,
                                color: menuType == "recent"
                                    ? Colors.black
                                    : Colors.black45,
                                fontWeight: FontWeight.bold,
                                fontFamily: "Gilroy"),
                          )),
                    )
                  ],
                ),
              ),
              ),
            ),
            menuType== "profile"? this.ProfileItems() : this.ReviewItems() : this.RecentItems(),
          ],
        ),
      ),
    );
  }
}

这就是我声明菜单类型的方式

final FirebaseAuth _auth = FirebaseAuth.instance;
  Firestore firestore = Firestore.instance;
  var _name, _uid, _phone, _language, _location, _image, menuType = "profile";
  Language language;

我无法显示这 3 个标签。当我删除最近的小部件时,它开始显示。 显示错误:

The following assertion was thrown building UserProfilePage(dirty, dependencies: [MediaQuery, _LocalizationsScope-[GlobalKey#47da7]], state: _UserProfilePageState#87b63):
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 285 pos 10: 'data != null'


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=BUG.md

The relevant error-causing widget was: 
  UserProfilePage file:///C:/Flutter/Naniz_eats/lib/main.dart:166:59
When the exception was thrown, this was the stack: 
#2      new Text (package:flutter/src/widgets/text.dart:285:10)
#3      _UserProfilePageState.build (package:econoomaccess/UserProfilePage.dart:708:34)
#4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)
#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)
#6      Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
...

【问题讨论】:

  • 贴出的代码是UserProfilePage.dart
  • 所有这些 sn-ps 都是 userprofilepage.dart 的一部分
  • 这是整个代码的链接:docs.google.com/document/d/…
  • 你能指点我到第 708 行吗?
  • 它可能在代码子的末尾是这样的:Text(getTranslated(context, "recent"), style: TextStyle(

标签: firebase flutter dart flutter-layout flutter-dependencies


【解决方案1】:

您的 AppLocalization 中似乎不存在该文本,因此您应该像这样进行 null 检查

String getTranslated(BuildContext context, String key) { 
   return AppLocalizations.of(context).translate(key) ?? "**text not found"; 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 2020-06-09
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多