Advertisement
Guest User

Untitled

a guest
Jul 30th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.49 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_spacex_upcoming/model/launch_response.dart';
  3. import 'package:flutter_spacex_upcoming/service/api_service.dart';
  4. import 'package:provider/provider.dart';
  5.  
  6. class DetailScreen extends StatefulWidget {
  7.   final int flightNumber;
  8.  
  9.   DetailScreen({this.flightNumber});
  10.  
  11.   @override
  12.   _DetailScreenState createState() => _DetailScreenState();
  13. }
  14.  
  15. class _DetailScreenState extends State<DetailScreen> {
  16.   @override
  17.   Widget build(BuildContext context) {
  18.     var apiService = ApiService();
  19.     return FutureProvider<Launch>(
  20.         create: (_) => apiService.getLaunchByFlightNumber(widget.flightNumber),
  21.         child: Container(
  22.           child: Text(context.watch<Launch>().details),
  23.         ),
  24.     );
  25.   }
  26. }
  27.  
  28.  
  29. =======================================================================================
  30.  
  31. he following ProviderNotFoundException was thrown building DetailScreen(dirty, state: _DetailScreenState#9933c):
  32. Error: Could not find the correct Provider<Launch> above this DetailScreen Widget
  33.  
  34. This likely happens because you used a `BuildContext` that does not include the provider
  35. of your choice. There are a few common scenarios:
  36.  
  37. - The provider you are trying to read is in a different route.
  38.  
  39.   Providers are "scoped". So if you insert of provider inside a route, then
  40.   other routes will not be able to access that provider.
  41.  
  42. - You used a `BuildContext` that is an ancestor of the provider you are trying to read.
  43.  
  44.   Make sure that DetailScreen is under your MultiProvider/Provider<Launch>.
  45.   This usually happen when you are creating a provider and trying to read it immediatly.
  46.  
  47.   For example, instead of:
  48.  
  49.   ```
  50.   Widget build(BuildContext context) {
  51.     return Provider<Example>(
  52.       create: (_) => Example(),
  53.       // Will throw a ProviderNotFoundError, because `context` is associated
  54.       // to the widget that is the parent of `Provider<Example>`
  55.       child: Text(context.watch<Example>()),
  56.     ),
  57.   }
  58.   ```
  59.  
  60.   consider using `builder` like so:
  61.  
  62.   ```
  63.   Widget build(BuildContext context) {
  64.     return Provider<Example>(
  65.       create: (_) => Example(),
  66.       // we use `builder` to obtain a new `BuildContext` that has access to the provider
  67.       builder: (context) {
  68.         // No longer throws
  69.         return Text(context.watch<Example>()),
  70.       }
  71.     ),
  72.   }
  73.   ```
  74.  
  75. If none of these solutions work, consider asking for help on StackOverflow:
  76. https://stackoverflow.com/questions/tagged/flutter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement