У flutter_shadcn проблемы с BlocProviderAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 У flutter_shadcn проблемы с BlocProvider

Сообщение Anonymous »

Я использую shadcn_flutter в своем проекте flutter, он использует несколько Cubit, и я использую get_it для создания поставщиков, мой файл main.dart выглядит так :

Код: Выделить всё

main.dart

Код: Выделить всё

import 'features/main_injection_container.dart' as di;

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.android,
);
await di.init();
runApp(
ShadcnApp(
theme: ThemeData(
colorScheme: ColorSchemes.darkNeutral(),
radius: 0.5,
),
home: const MainApp(),
),
);
}

class MainApp extends StatelessWidget {
const MainApp({super.key});

@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => di.sl(),
),
BlocProvider(
create: (context) => di.sl(),
),
BlocProvider(
create: (context) => di.sl(),
),
],
child: const SignUp(),
);
}
}
и затем, когда я пытаюсь выполнить Navigator.of(context).push(MaterialPageRoute(builder: (context) => Login())), я получаю следующее ошибка:

Код: Выделить всё

════════ Exception caught by widgets library ═══════════════════════════════════
The following ProviderNotFoundException was thrown building Login(state: _LoginState#12790):
Error: Could not find the correct Provider above this BlocBuilder Widget

This happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:

- You added a new provider in your `main.dart` and performed a hot-reload.
To fix, perform a hot-restart.

- The provider you are trying to read is in a different route.

Providers are "scoped". So if you insert of provider inside a route, then
other routes will not be able to access that provider.

- You used a `BuildContext` that is an ancestor of the provider you are trying to read.

Make sure that BlocBuilder is under your MultiProvider/Provider.
This usually happens when you are creating a provider and trying to read it immediately.

For example, instead of:

Widget build(BuildContext context) {
return Provider(
create: (_) => Example(),
// Will throw a ProviderNotFoundError, because `context` is associated
// to the widget that is the parent of `Provider`
child: Text(context.watch().toString()),
);
}

consider using `builder` like so:

Widget build(BuildContext context) {
return Provider(
create: (_) => Example(),
// we use `builder` to obtain a new `BuildContext` that has access to the provider
builder: (context, child) {
// No longer throws
return Text(context.watch().toString());
}
);
}

If none of these solutions work, consider asking for help on StackOverflow:
https://stackoverflow.com/questions/tagged/flutter

The relevant error-causing widget was:
Login Login:file:///Users/sami/Documents/flutter_projects/chacha_caller/lib/features/user/presentation/pages/login_signup/signup.dart:152:71
То же самое произойдет, если я добавлю страницу Login() в файл main.dart, а затем Navigator.push() в файл Страница SignUp().

Подробнее здесь: https://stackoverflow.com/questions/790 ... ocprovider
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»