💻 آخرین فرصت یادگیری برنامه‌نویسی با آفر ویژه قبل از افزایش قیمت در ۵ آذر ماه (🎁 به همراه یک هدیه ارزشمند )
۰ ثانیه
۰ دقیقه
۰ ساعت
۷ محمدرضا حبیبی فتح آبادی
exception when click on Add New Task
جامعه فلاتر ایجاد شده در ۱۷ دی ۱۴۰۲

وقتی روی add newTask کلیک می‌کنم با این استثنا رو به رو میشم:

 

Exception has occurred.

  •  

ProviderNotFoundException (Error: Could not find the correct Provider<EditTaskCubit> above this EditTaskScreen Widget This happens because you used a `BuildContext` that does not include the provider of your choice. There are a few common scenarios:

EditTaskCubit رو به کد معرفی کردید؟

تصویر کدتون و جایی که خطا میده رو هم ارسال کنید

یلدا محصلی ۱۷ دی ۱۴۰۲، ۱۵:۳۳
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:task_list/data/data.dart';
import 'package:task_list/main.dart';
import 'package:task_list/screens/edit/cubit/edit_task_cubit.dart';
class EditTaskScreen extends StatefulWidget {
  final TaskEntity task;
  EditTaskScreen({super.key, required this.task});
  @override
  State<EditTaskScreen> createState() => _EditTaskScreenState();
}
class _EditTaskScreenState extends State<EditTaskScreen> {
  late final TextEditingController _controller;
  @override
  void initState() {
    _controller = TextEditingController(
        text: context.read<EditTaskCubit>().state.task.name);
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    final ThemeData themeData = Theme.of(context);
    return Scaffold(
      backgroundColor: themeData.colorScheme.surface,
      appBar: AppBar(
        elevation: 0,
        title: Text('Edit Task'),
        backgroundColor: themeData.colorScheme.surface,
        foregroundColor: themeData.colorScheme.onSurface,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      floatingActionButton: FloatingActionButton.extended(
          onPressed: () {
            context.read<EditTaskCubit>().onSaveChangesClick();
            Navigator.of(context).pop();
          },
          label: Row(
            children: [
              Text('Save Changes'),
              Icon(
                CupertinoIcons.check_mark,
                size: 16,
              ),
            ],
          )),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            BlocBuilder<EditTaskCubit, EditTaskState>(
              builder: (context, state) {
                final priority = state.task.priority;
                return Flex(
                  direction: Axis.horizontal,
                  children: [
                    Flexible(
                        flex: 1,
                        child: PriorityCheckBox(
                          onTap: () {
                            context
                                .read<EditTaskCubit>()
                                .onPriorityChanged(Priority.high);
                          },
                          label: 'High',
                          color: primaryColor,
                          isSelected: priority == Priority.high,
                        )),
                    SizedBox(
                      width: 8,
                    ),
                    Flexible(
                        flex: 1,
                        child: PriorityCheckBox(
                          onTap: () {
                            context
                                .read<EditTaskCubit>()
                                .onPriorityChanged(Priority.normal);
                          },
                          label: 'Normal',
                          color: Color(0xfff09819),
                          isSelected: priority == Priority.normal,
                        )),
                    SizedBox(
                      width: 8,
                    ),
                    Flexible(
                        flex: 1,
                        child: PriorityCheckBox(
                          onTap: () {
                            context
                                .read<EditTaskCubit>()
                                .onPriorityChanged(Priority.low);
                          },
                          label: 'Low',
                          color: Color(0xff3be1f1),
                          isSelected: priority == Priority.low,
                        ))
                  ],
                );
              },
            ),
            TextField(
              controller: _controller,
              onChanged: (value) =>
                  context.read<EditTaskCubit>().onTextChanged(value),
              decoration: InputDecoration(label: Text('Add a Task for Today')),
            )
          ],
        ),
      ),
    );
  }
}
class PriorityCheckBox extends StatelessWidget {
  final String label;
  final Color color;
  final bool isSelected;
  final GestureTapCallback onTap;
  const PriorityCheckBox(
      {super.key,
      required this.label,
      required this.color,
      required this.isSelected,
      required this.onTap});
  @override
  Widget build(BuildContext context) {
    // final ThemeData themeData = Theme.of(context);
    return InkWell(
      onTap: onTap,
      child: Container(
        height: 40,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(4),
            border: Border.all(
                width: 2, color: secondaryTextColor.withOpacity(0.2))),
        child: Stack(
          children: [
            Center(child: Text(label)),
            Positioned(
              top: 0,
              right: 4,
              bottom: 0,
              child: Center(
                child: CheckBoxShape(
                  value: isSelected,
                  color: color,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
class CheckBoxShape extends StatelessWidget {
  final bool value;
  final Color color;
  const CheckBoxShape({super.key, required this.value, required this.color});
  @override
  Widget build(BuildContext context) {
    final ThemeData themeData = Theme.of(context);
    return Container(
      //margin: EdgeInsets.only(right: 16),
      width: 16,
      height: 16,
      decoration:
          BoxDecoration(borderRadius: BorderRadius.circular(12), color: color),
      child: value
          ? Icon(
              CupertinoIcons.checkmark,
              color: themeData.colorScheme.onPrimary,
              size: 12,
            )
          : null,
    );
  }
}
محمدرضا حبیبی فتح آبادی ۱۷ دی ۱۴۰۲، ۱۶:۳۵
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:task_list/data/data.dart';
import 'package:task_list/main.dart';
import 'package:task_list/screens/edit/cubit/edit_task_cubit.dart';
class EditTaskScreen extends StatefulWidget {
  final TaskEntity task;
  EditTaskScreen({super.key, required this.task});
  @override
  State<EditTaskScreen> createState() => _EditTaskScreenState();
}
class _EditTaskScreenState extends State<EditTaskScreen> {
  late final TextEditingController _controller;
  @override
  void initState() {
    _controller = TextEditingController(
        text: context.read<EditTaskCubit>().state.task.name);
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    final ThemeData themeData = Theme.of(context);
    return Scaffold(
      backgroundColor: themeData.colorScheme.surface,
      appBar: AppBar(
        elevation: 0,
        title: Text('Edit Task'),
        backgroundColor: themeData.colorScheme.surface,
        foregroundColor: themeData.colorScheme.onSurface,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      floatingActionButton: FloatingActionButton.extended(
          onPressed: () {
            context.read<EditTaskCubit>().onSaveChangesClick();
            Navigator.of(context).pop();
          },
          label: Row(
            children: [
              Text('Save Changes'),
              Icon(
                CupertinoIcons.check_mark,
                size: 16,
              ),
            ],
          )),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            BlocBuilder<EditTaskCubit, EditTaskState>(
              builder: (context, state) {
                final priority = state.task.priority;
                return Flex(
                  direction: Axis.horizontal,
                  children: [
                    Flexible(
                        flex: 1,
                        child: PriorityCheckBox(
                          onTap: () {
                            context
                                .read<EditTaskCubit>()
                                .onPriorityChanged(Priority.high);
                          },
                          label: 'High',
                          color: primaryColor,
                          isSelected: priority == Priority.high,
                        )),
                    SizedBox(
                      width: 8,
                    ),
                    Flexible(
                        flex: 1,
                        child: PriorityCheckBox(
                          onTap: () {
                            context
                                .read<EditTaskCubit>()
                                .onPriorityChanged(Priority.normal);
                          },
                          label: 'Normal',
                          color: Color(0xfff09819),
                          isSelected: priority == Priority.normal,
                        )),
                    SizedBox(
                      width: 8,
                    ),
                    Flexible(
                        flex: 1,
                        child: PriorityCheckBox(
                          onTap: () {
                            context
                                .read<EditTaskCubit>()
                                .onPriorityChanged(Priority.low);
                          },
                          label: 'Low',
                          color: Color(0xff3be1f1),
                          isSelected: priority == Priority.low,
                        ))
                  ],
                );
              },
            ),
            TextField(
              controller: _controller,
              onChanged: (value) =>
                  context.read<EditTaskCubit>().onTextChanged(value),
              decoration: InputDecoration(label: Text('Add a Task for Today')),
            )
          ],
        ),
      ),
    );
  }
}
class PriorityCheckBox extends StatelessWidget {
  final String label;
  final Color color;
  final bool isSelected;
  final GestureTapCallback onTap;
  const PriorityCheckBox(
      {super.key,
      required this.label,
      required this.color,
      required this.isSelected,
      required this.onTap});
  @override
  Widget build(BuildContext context) {
    // final ThemeData themeData = Theme.of(context);
    return InkWell(
      onTap: onTap,
      child: Container(
        height: 40,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(4),
            border: Border.all(
                width: 2, color: secondaryTextColor.withOpacity(0.2))),
        child: Stack(
          children: [
            Center(child: Text(label)),
            Positioned(
              top: 0,
              right: 4,
              bottom: 0,
              child: Center(
                child: CheckBoxShape(
                  value: isSelected,
                  color: color,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
class CheckBoxShape extends StatelessWidget {
  final bool value;
  final Color color;
  const CheckBoxShape({super.key, required this.value, required this.color});
  @override
  Widget build(BuildContext context) {
    final ThemeData themeData = Theme.of(context);
    return Container(
      //margin: EdgeInsets.only(right: 16),
      width: 16,
      height: 16,
      decoration:
          BoxDecoration(borderRadius: BorderRadius.circular(12), color: color),
      child: value
          ? Icon(
              CupertinoIcons.checkmark,
              color: themeData.colorScheme.onPrimary,
              size: 12,
            )
          : null,
    );
  }
}
محمدرضا حبیبی فتح آبادی ۱۷ دی ۱۴۰۲، ۱۶:۳۵
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:task_list/data/data.dart';
import 'package:task_list/main.dart';
import 'package:task_list/screens/edit/cubit/edit_task_cubit.dart';
class EditTaskScreen extends StatefulWidget {
  final TaskEntity task;
  EditTaskScreen({super.key, required this.task});
  @override
  State<EditTaskScreen> createState() => _EditTaskScreenState();
}
class _EditTaskScreenState extends State<EditTaskScreen> {
  late final TextEditingController _controller;
  @override
  void initState() {
    _controller = TextEditingController(
        text: context.read<EditTaskCubit>().state.task.name);
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    final ThemeData themeData = Theme.of(context);
    return Scaffold(
      backgroundColor: themeData.colorScheme.surface,
      appBar: AppBar(
        elevation: 0,
        title: Text('Edit Task'),
        backgroundColor: themeData.colorScheme.surface,
        foregroundColor: themeData.colorScheme.onSurface,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      floatingActionButton: FloatingActionButton.extended(
          onPressed: () {
            context.read<EditTaskCubit>().onSaveChangesClick();
            Navigator.of(context).pop();
          },
          label: Row(
            children: [
              Text('Save Changes'),
              Icon(
                CupertinoIcons.check_mark,
                size: 16,
              ),
            ],
          )),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            BlocBuilder<EditTaskCubit, EditTaskState>(
              builder: (context, state) {
                final priority = state.task.priority;
                return Flex(
                  direction: Axis.horizontal,
                  children: [
                    Flexible(
                        flex: 1,
                        child: PriorityCheckBox(
                          onTap: () {
                            context
                                .read<EditTaskCubit>()
                                .onPriorityChanged(Priority.high);
                          },
                          label: 'High',
                          color: primaryColor,
                          isSelected: priority == Priority.high,
                        )),
                    SizedBox(
                      width: 8,
                    ),
                    Flexible(
                        flex: 1,
                        child: PriorityCheckBox(
                          onTap: () {
                            context
                                .read<EditTaskCubit>()
                                .onPriorityChanged(Priority.normal);
                          },
                          label: 'Normal',
                          color: Color(0xfff09819),
                          isSelected: priority == Priority.normal,
                        )),
                    SizedBox(
                      width: 8,
                    ),
                    Flexible(
                        flex: 1,
                        child: PriorityCheckBox(
                          onTap: () {
                            context
                                .read<EditTaskCubit>()
                                .onPriorityChanged(Priority.low);
                          },
                          label: 'Low',
                          color: Color(0xff3be1f1),
                          isSelected: priority == Priority.low,
                        ))
                  ],
                );
              },
            ),
            TextField(
              controller: _controller,
              onChanged: (value) =>
                  context.read<EditTaskCubit>().onTextChanged(value),
              decoration: InputDecoration(label: Text('Add a Task for Today')),
            )
          ],
        ),
      ),
    );
  }
}
class PriorityCheckBox extends StatelessWidget {
  final String label;
  final Color color;
  final bool isSelected;
  final GestureTapCallback onTap;
  const PriorityCheckBox(
      {super.key,
      required this.label,
      required this.color,
      required this.isSelected,
      required this.onTap});
  @override
  Widget build(BuildContext context) {
    // final ThemeData themeData = Theme.of(context);
    return InkWell(
      onTap: onTap,
      child: Container(
        height: 40,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(4),
            border: Border.all(
                width: 2, color: secondaryTextColor.withOpacity(0.2))),
        child: Stack(
          children: [
            Center(child: Text(label)),
            Positioned(
              top: 0,
              right: 4,
              bottom: 0,
              child: Center(
                child: CheckBoxShape(
                  value: isSelected,
                  color: color,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
class CheckBoxShape extends StatelessWidget {
  final bool value;
  final Color color;
  const CheckBoxShape({super.key, required this.value, required this.color});
  @override
  Widget build(BuildContext context) {
    final ThemeData themeData = Theme.of(context);
    return Container(
      //margin: EdgeInsets.only(right: 16),
      width: 16,
      height: 16,
      decoration:
          BoxDecoration(borderRadius: BorderRadius.circular(12), color: color),
      child: value
          ? Icon(
              CupertinoIcons.checkmark,
              color: themeData.colorScheme.onPrimary,
              size: 12,
            )
          : null,
    );
  }
}
محمدرضا حبیبی فتح آبادی ۱۷ دی ۱۴۰۲، ۱۶:۳۵

دقیقا کدوم قسمت به ارور خوردید؟

یلدا محصلی ۱۷ دی ۱۴۰۲، ۱۹:۵۴

توی قسمت () initState

محمدرضا حبیبی فتح آبادی ۱۸ دی ۱۴۰۲، ۰۴:۵۸

مشابه این قسمت ویدیو (۱۳:۵۰) برای  push به این صفحه از BlocProvider استفاده کردید؟ 

یلدا محصلی ۱۸ دی ۱۴۰۲، ۱۴:۱۴