Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:flutter_spacex_upcoming/model/launch_response.dart';
- import 'package:flutter_spacex_upcoming/service/api_service.dart';
- import 'package:provider/provider.dart';
- class DetailScreen extends StatefulWidget {
- final int flightNumber;
- DetailScreen({this.flightNumber});
- @override
- _DetailScreenState createState() => _DetailScreenState();
- }
- class _DetailScreenState extends State<DetailScreen> {
- @override
- Widget build(BuildContext context) {
- var apiService = ApiService();
- return FutureProvider<Launch>(
- create: (_) => apiService.getLaunchByFlightNumber(widget.flightNumber),
- child: Container(
- child: Text(context.watch<Launch>().details),
- ),
- );
- }
- }
- =======================================================================================
- he following ProviderNotFoundException was thrown building DetailScreen(dirty, state: _DetailScreenState#9933c):
- Error: Could not find the correct Provider<Launch> above this DetailScreen Widget
- This likely happens because you used a `BuildContext` that does not include the provider
- of your choice. There are a few common scenarios:
- - 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 DetailScreen is under your MultiProvider/Provider<Launch>.
- This usually happen when you are creating a provider and trying to read it immediatly.
- For example, instead of:
- ```
- Widget build(BuildContext context) {
- return Provider<Example>(
- create: (_) => Example(),
- // Will throw a ProviderNotFoundError, because `context` is associated
- // to the widget that is the parent of `Provider<Example>`
- child: Text(context.watch<Example>()),
- ),
- }
- ```
- consider using `builder` like so:
- ```
- Widget build(BuildContext context) {
- return Provider<Example>(
- create: (_) => Example(),
- // we use `builder` to obtain a new `BuildContext` that has access to the provider
- builder: (context) {
- // No longer throws
- return Text(context.watch<Example>()),
- }
- ),
- }
- ```
- If none of these solutions work, consider asking for help on StackOverflow:
- https://stackoverflow.com/questions/tagged/flutter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement