-1

I have create a cubit using bloc provider in the Chapters screen and in another screen I'm trying to consume the same cubit directly without creating another one.

this is the Chapters screen

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:manga_series/modules/manga/arabic_chapters.dart';
import 'package:manga_series/modules/manga/cubit/cubit.dart';
import 'package:manga_series/modules/manga/cubit/states.dart';
import 'package:manga_series/modules/manga/english_chapters.dart';
import 'package:manga_series/shared/style/colors.dart';

class Chapters extends StatelessWidget {
  const Chapters({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (BuildContext context) => MangaScreenCubit(),
      child: BlocConsumer<MangaScreenCubit, MangaScreenStates>(
        listener: (context, state) => {},
        builder: (context, state) {
          var cubit = MangaScreenCubit.get(context);
          return DefaultTabController(
            initialIndex: cubit.chaptersLanguage,
            length: 2,
            child: SingleChildScrollView(
              physics: BouncingScrollPhysics(),
              scrollDirection: Axis.vertical,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 8),
                    child: Row(
                      children: [
                        Expanded(
                          child: Text(
                            "Chapters Number : 54",
                            style: TextStyle(
                                fontSize: 12, fontWeight: FontWeight.w500),
                          ),
                        ),
                        TextButton(
                            onPressed: () {
                              cubit.changeChaptersLanguageEnglish();
                            },
                            child: Text(
                              "English",
                              style: TextStyle(
                                  fontSize: 12,
                                  color: cubit.chaptersLanguage == 0
                                      ? primaryColor
                                      : Colors.grey),
                            )),
                        TextButton(
                            onPressed: () {
                              cubit.changeChaptersLanguageArabic();
                            },
                            child: Text(
                              "Arabic",
                              style: TextStyle(
                                  fontSize: 12,
                                  color: cubit.chaptersLanguage == 1
                                      ? primaryColor
                                      : Colors.grey),
                            )),
                      ],
                    ),
                  ),
                  IndexedStack(
                    children: <Widget>[
                      Visibility(
                        child: EnglishChapters(),
                        maintainState: true,
                        visible: cubit.chaptersLanguage == 0,
                      ),
                      Visibility(
                        child: ArabicChapters(),
                        maintainState: true,
                        visible: cubit.chaptersLanguage == 1,
                      ),
                    ],
                    index: cubit.chaptersLanguage,
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}

On the second page Im trying to consume the cubit without using bloc provider. But i got this error

        _InheritedProviderScope<T?>>() as _InheritedProviderScopeElement<T?>?;

    if (inheritedElement == null && null is! T) {
      throw ProviderNotFoundException(T, context.widget.runtimeType);
    }

    return inheritedElement;
  }

and this the page where im trying to consume the cubit

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:manga_series/modules/manga/cubit/cubit.dart';
import 'package:manga_series/modules/manga/cubit/states.dart';
import 'package:manga_series/shared/components/components.dart';
import 'package:readmore/readmore.dart';

class Descreption extends StatelessWidget {
  Descreption({Key? key}) : super(key: key);

  String descreption =
      "Lorem ipsum dolor sit amet, ea sed quis utinam invidunt, nec no tantas aeterno, at quot insolens mea. Dicta menandri per te, vim ad summo antiopam imperdiet, eu nostro repudiare cum. Ex aliquando eloquentiam quo, at mea quem phaedrum convenire. Eu vel legendos assentior repudiandae, vix te case accumsan suscipit.Tale etiam indoctum vel ei. Cu nam velit altera delectus. Graeci detraxit vix ad, an pri solum summo vivendo. Eu liber dolore intellegebat vis. Vis facilis suavitate instructior ut, et ullum periculis hendrerit vim. Te sonet albucius legendos mel, simul labores an vel.Lorem ipsum dolor sit amet, ea sed quis utinam invidunt, nec no tantas aeterno, at quot insolens mea. Dicta menandri per te, vim ad summo antiopam imperdiet, eu nostro repudiare cum. Ex aliquando eloquentiam quo, at mea quem phaedrum convenire. Eu vel legendos assentior repudiandae, vix te case accumsan suscipit.Tale etiam indoctum vel ei. Cu nam velit altera delectus. Graeci detraxit vix ad, an pri solum summo vivendo. Eu liber dolore intellegebat vis. Vis facilis suavitate instructior ut, et ullum periculis hendrerit vim. Te sonet albucius legendos mel, simul labores an vel.Lorem ipsum dolor sit amet, ea sed quis utinam invidunt, nec no tantas aeterno, at quot insolens mea. Dicta menandri per te, vim ad summo antiopam imperdiet, eu nostro repudiare cum. Ex aliquando eloquentiam quo, at mea quem phaedrum convenire. Eu vel legendos assentior repudiandae, vix te case accumsan suscipit.Tale etiam indoctum vel ei. Cu nam velit altera delectus. Graeci detraxit vix ad, an pri solum summo vivendo. Eu liber dolore intellegebat vis. Vis facilis suavitate instructior ut, et ullum periculis hendrerit vim. Te sonet albucius legendos mel, simul labores an vel.Lorem ipsum dolor sit amet, ea sed quis utinam invidunt, nec no tantas aeterno, at quot insolens mea. Dicta menandri per te, vim ad summo antiopam imperdiet, eu nostro repudiare cum. Ex aliquando eloquentiam quo, at mea quem phaedrum convenire. Eu vel legendos assentior repudiandae, vix te case accumsan suscipit.Tale etiam indoctum vel ei. Cu nam velit altera delectus. Graeci detraxit vix ad, an pri solum summo vivendo. Eu liber dolore intellegebat vis. Vis facilis suavitate instructior ut, et ullum periculis hendrerit vim. Te sonet albucius legendos mel, simul labores an vel.";

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<MangaScreenCubit, MangaScreenStates>(
      listener: (context, state) => {},
      builder: (context, state) {
        return Padding(
          padding: const EdgeInsets.symmetric(
            horizontal: 15,
          ),
          child: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            physics: const BouncingScrollPhysics(),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const SizedBox(
                  height: 10,
                ),
                const Text(
                  "The Beginning After The End",
                  textWidthBasis: TextWidthBasis.longestLine,
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(fontSize: 22, fontWeight: FontWeight.w600),
                ),
                const SizedBox(
                  height: 8,
                ),
                Row(
                  children: [
                    Row(
                      children: const [
                        Icon(
                          Icons.fireplace_outlined,
                          color: Colors.grey,
                          size: 20,
                        ),
                        SizedBox(
                          width: 5,
                        ),
                        Text(
                          "154.3K",
                          style: TextStyle(
                            fontSize: 13,
                            color: Colors.grey,
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(
                      width: 15,
                    ),
                    Row(
                      children: const [
                        Icon(
                          Icons.remove_red_eye_outlined,
                          size: 20,
                          color: Colors.grey,
                        ),
                        SizedBox(
                          width: 5,
                        ),
                        Text(
                          "257.9K",
                          style: TextStyle(
                            fontSize: 13,
                            color: Colors.grey,
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(
                      width: 15,
                    ),
                    Row(
                      children: const [
                        Icon(
                          Icons.star,
                          size: 20,
                          color: Colors.orangeAccent,
                        ),
                        Text(
                          "4",
                          style: TextStyle(
                            fontSize: 13,
                            color: Colors.grey,
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
                const SizedBox(
                  height: 15,
                ),
                Row(
                  children: [
                    mangaInfo(text: "Completed"),
                    const SizedBox(width: 5),
                    mangaInfo(text: "English"),
                    const SizedBox(width: 5),
                    mangaInfo(text: "Arabic"),
                  ],
                ),
                const SizedBox(
                  height: 20,
                ),
                const Text(
                  "Update On Monday",
                  style: TextStyle(
                      color: Colors.black87,
                      fontSize: 14,
                      fontWeight: FontWeight.bold),
                ),
                const SizedBox(
                  height: 5,
                ),
                ReadMoreText(
                  descreption,
                  trimLines: 4,
                  trimMode: TrimMode.Line,
                  moreStyle: const TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: Color.fromARGB(255, 6, 193, 71),
                  ),
                  lessStyle: const TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: Color.fromARGB(255, 6, 193, 71),
                  ),
                ),
                const SizedBox(
                  height: 15,
                ),
                SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  physics: const BouncingScrollPhysics(),
                  child: Row(
                    children: [
                      mangaInfo(text: "Action"),
                      const SizedBox(width: 5),
                      mangaInfo(text: "Adventure"),
                      const SizedBox(width: 5),
                      mangaInfo(text: "Fantasy"),
                      const SizedBox(width: 5),
                      mangaInfo(text: "Magic"),
                      const SizedBox(width: 5),
                      mangaInfo(text: "Shounen"),
                    ],
                  ),
                ),
                const SizedBox(
                  height: 30,
                ),
                secondaryButton(
                  text: "My List",
                  width: 50.0,
                  icon: Icons.add,
                  onPressed: () {},
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}

Widget createButton({icon, color, text}) {
  return ButtonTheme(
    minWidth: 140,
    height: 45,
    child: FlatButton.icon(
      onPressed: () {},
      icon: icon,
      label: Text(
        text,
        style: const TextStyle(color: Colors.white),
      ),
      color: color,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
    ),
  );
}

Widget mangaInfo({required String text}) {
  return Container(
    height: 25,
    width: 85,
    decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(20), color: Colors.grey[300]),
    child: Center(
        child: Text(text,
            style: TextStyle(
                fontSize: 12,
                color: Colors.grey[700],
                fontWeight: FontWeight.bold))),
  );
}

anikki00
  • 11
  • 2

1 Answers1

0

I think your issue isn't related to the second screen. Look at this code you've written

class Chapters extends StatelessWidget {
  const Chapters({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (BuildContext context) => MangaScreenCubit(),
      child: BlocConsumer<MangaScreenCubit, MangaScreenStates>(
        listener: (context, state) => {},
        builder: (context, state) {
          var cubit = MangaScreenCubit.get(context);

You create MangaScreenCubit and right after that using BlocConsumer<MangaScreenCubit, MangaScreenStates>. But you can't do this because current context does not have MangaScreenCubit provided. Try to separate cubit provisioning from consuming:

class Chapters extends StatelessWidget {
  const Chapters({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (BuildContext context) => MangaScreenCubit(),
      child: ChaptersWidget(),
    );
  }
}

class ChaptersWidget extends StatelessWidget {
  const ChaptersWidget({Key? key}) : super(key: key);

  Widget build(BuildContext context) {
    return BlocConsumer<MangaScreenCubit, MangaScreenStates>(
        listener: (context, state) => {},
        builder: (context, state) {
        /// rest of code
        }
    );
  }
}
Alex Verbitski
  • 499
  • 3
  • 12