Flutter GridView 不显示孩子:失败的断言:第 4269 行 pos 14:'owner!._debugCurrentBuildTarget == this':不是真的

时间:2021-06-11 22:17:51

标签: flutter

所以我在我的待办事项应用程序中有这个主屏幕,其中待办事项出现在网格视图中,出于某种原因,我的手机屏幕被完全读取,我不断收到以下错误:

'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
The relevant error-causing widget was
GridView
lib\src\my_app.dart:72

这是我主屏幕上的代码:

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final TodoDataManager data = TodoDataManager();
  final ToDosFilterer filterer = ToDosFilterer();
  ScrollController _controller;
  double _floatingButtonSize = 60.0;
  List allData = [];

  @override
  void initState() {
    super.initState();
    _controller = ScrollController();
    _controller.addListener(() {
      if (_controller.offset >= _controller.position.maxScrollExtent)
        setState(() => _floatingButtonSize = 0.0);
      else
        setState(() => _floatingButtonSize = 60.0);
    });
  }

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

  // Widget filterdTodosSection(List data, String title) {
  //   return Column(
  //     children: [
  //       TodoCardsList(
  //         title: title,
  //         todos: data,
  //       ),
  //       SizedBox(height: data.length == 0 ? 0.0 : 40.0),
  //     ],
  //   );
  // }

  @override
  Widget build(BuildContext context) {
    final todoDataProvider = Provider.of<TodoModel>(context);
    final themeProvider = Provider.of<ClrTheme>(context);
    final todoSelectedForChangeProvider =
        Provider.of<TodoActionSelection>(context);
    final selectedTodosProvider =
        Provider.of<SelectedTodosProvider>(context, listen: false);
    // final cardsRefresherProvider =
    //     Provider.of<TodoCardsRefresherProvider>(context);
    // (cardsRefresherProvider.isRefreshed)
    //     ? setState(() {
    //         allData.clear();
    //         print('good');
    //       })
    //     : print("sorry, no setstate");
    return WillPopScope(
      onWillPop: () {
        SystemNavigator.pop();
        return Future.value(false);
      },
      child: Scaffold(
        appBar: AppBar(
          title: Text(
            "todo".toUpperCase(),
            style: Theme.of(context).textTheme.headline3,
          ),
          centerTitle: true,
          actions: todoSelectedForChangeProvider.isTodoSelected
              ? [
                  IconButton(
                    icon: Icon(Icons.delete,
                        color: themeProvider.isdarkThemeOn()
                            ? whiteColor
                            : blackColor),
                    onPressed: () {},
                  ),
                  IconButton(
                    icon: Icon(Icons.edit,
                        color: themeProvider.isdarkThemeOn()
                            ? whiteColor
                            : blackColor),
                    onPressed: () {},
                  ),
                ]
              : [
                  IconButton(
                    icon: Icon(Icons.settings,
                        color: themeProvider.isdarkThemeOn()
                            ? whiteColor
                            : blackColor),
                    onPressed: () {
                      // todoDataProvider.resetAll();
                      Navigator.pushNamed(context, "/settings");
                    },
                  ),
                ],
          leadingWidth: 40,
          leading: Padding(
              padding: const EdgeInsets.only(left: 8.0),
              child: todoSelectedForChangeProvider.isTodoSelected
                  ? IconButton(
                      icon: Icon(Icons.cancel_sharp,
                          color: themeProvider.isdarkThemeOn()
                              ? whiteColor
                              : blackColor),
                      onPressed: () {
                        todoSelectedForChangeProvider.setTodoSelected = false;
                        selectedTodosProvider.resetTodosCount();
                      },
                    )
                  : SizedBox.shrink()),
        ),
        floatingActionButton: AnimatedContainer(
          duration: Duration(milliseconds: 400),
          curve: Curves.easeIn,
          width: _floatingButtonSize,
          height: _floatingButtonSize,
          child: MaterialButton(
            elevation: 8.0,
            color: Theme.of(context).primaryColor,
            child: Text(
              "+",
              style: TextStyle(fontSize: _floatingButtonSize < 40 ? 0.0 : 40),
            ),
            onPressed: () {
              todoDataProvider.resetAll();
              Navigator.pushNamed(context, "/todo", arguments: ["add"]).then(
                (value) => setState(() {}),
              );
            },
          ),
        ),
        drawer: CustomedDrawer(),
        body: FutureBuilder(
          future: data.getTodoData(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (snapshot.hasData) {
              allData.clear();
              allData.addAll(snapshot.data);
              print("alldata from db ${snapshot.data}");
              print("alldata not from db $allData");
              return snapshot.data.isEmpty
                  ? HomePlaceHolder()
                  : Column(
                      children: [
                        const SizedBox(height: 10.0),
                        Column(
                          children: [
                            WorkoutTodosButton(),
                            const SizedBox(height: 20),
                            Container(
                              padding: const EdgeInsets.only(left: 10.0),
                              alignment: Alignment.centerLeft,
                              child: Text(
                                "${snapshot.data.length} " +
                                    "TODO(s) Of Today" +
                                    ":",
                                style: Theme.of(context)
                                    .textTheme
                                    .headline4
                                    .copyWith(fontSize: 16),
                              ),
                            ),
                          ],
                        ),
                        Expanded(
                          child: GridView.count(
                            padding: const EdgeInsets.fromLTRB(10, 20, 10, 10),
                            crossAxisSpacing: 24,
                            mainAxisSpacing: 24,
                            childAspectRatio: 0.6,
                            crossAxisCount: 3,
                            children: snapshot.data
                                .map((todo) => TodoCard(
                                    todoData: todo)) //TodoCard(todoData: todo)
                                .toList(),
                          ),
                        ),
                      ],
                    );
            } else if (snapshot.hasError) {
              print("something went wrong!");
            }
            return LoadingSpinner();
          },
        ),
      ),
    );
  }
}

这是TodoCard类的代码

class TodoCard extends StatefulWidget {
  final TodoData todoData;

  TodoCard({this.todoData});

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

class _TodoCardState extends State<TodoCard> {
  // int currentIndex;
  ShowDetails detailsProvider;
  TodoActionSelection todoSelectedForChangeProvider;
  SelectedTodosProvider selectedTodosProvider;
  Color _borderColor = whiteColor;

  @override
  void initState() {
    super.initState();
    detailsProvider = Provider.of<ShowDetails>(context);
    todoSelectedForChangeProvider = Provider.of<TodoActionSelection>(context);
    selectedTodosProvider =
        Provider.of<SelectedTodosProvider>(context, listen: false);
  }

  @override
  Widget build(BuildContext context) {
    return IgnorePointer(
      ignoring: todoSelectedForChangeProvider.isTodoSelected ? true : false,
      child: GestureDetector(
        onLongPress: () {
          selectedTodosProvider
            ..incrementTodosCount()
            ..addToSelectedTodosIDs(this.widget.todoData.id);
        },
        child: Container(
          height: 100,
          width: 80,
          decoration: BoxDecoration(
            border: Border.all(color: _borderColor),
            boxShadow: [
              BoxShadow(
                  color: Colors.black54,
                  blurRadius: 15.0,
                  offset: Offset(0.0, 0.75)),
            ],
          ),
          child: Stack(
            fit: StackFit.expand,
            children: [
              Image.asset(
                "assets/images/${this.widget.todoData.targetedArea}.jpg",
                fit: BoxFit.fill,
              ),
              Container(
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    begin: Alignment.bottomCenter,
                    end: Alignment.topCenter,
                    colors: [Theme.of(context).primaryColor, transparentColor],
                    stops: [0.2, 1],
                  ),
                ),
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Align(
                    alignment: Alignment.topRight,
                    child: Padding(
                      padding: const EdgeInsets.all(2.0),
                      child: DoneCheckBox(
                          onChecked: () {}, //this.widget.onCompleted,
                          todoId: this.widget.todoData.id),
                    ),
                  ),
                  Flexible(
                    child: Text(
                      this.widget.todoData.title,
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 25,
                        color: silverColor,
                      ),
                      textAlign: TextAlign.center,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                  Column(
                    children: [
                      Container(
                        height: 40,
                        width: double.infinity,
                        decoration: BoxDecoration(
                          color: Theme.of(context).accentColor,
                          border: Border.symmetric(
                              horizontal: BorderSide(
                                  color: todoSelectedForChangeProvider
                                          .isTodoSelected
                                      ? greenColor
                                      : whiteColor)),
                        ),
                        child: Material(
                          color: transparentColor,
                          child: InkWell(
                            onTap: () {
                              detailsProvider.setShowDetails = false;
                              showDialog(
                                context: context,
                                builder: (context) =>
                                    InfoPopup(todoData: this.widget.todoData),
                              );
                            },
                            child: Center(
                              child: Icon(
                                Icons.remove_red_eye,
                                size: 30,
                              ),
                            ),
                          ),
                        ),
                      ),
                      SizedBox(height: 10.0),
                      Container(
                        margin: const EdgeInsets.only(bottom: 5.0),
                        child: Text(
                          this.widget.todoData.targetedArea.toUpperCase(),
                          style: Theme.of(context).textTheme.headline3.copyWith(
                            color: whiteColor,
                            shadows: [
                              BoxShadow(
                                color: blackColor,
                                offset: Offset(0, 3),
                                spreadRadius: 5,
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
              todoSelectedForChangeProvider.isTodoSelected
                  ? Container(
                      color: _borderColor, //Colors.green.withOpacity(0.3),
                      width: double.infinity,
                      height: double.infinity,
                    )
                  : SizedBox.shrink(),
            ],
          ),
        ),
      ),
    );
  }
}

如果可以,请帮忙,这个错误让我几天都忽略了开发这个应用程序

0 个答案:

没有答案