Flutter 中的提供程序出现错误 "Could not find the correct provider above this X Widget"

Provider in Flutter with error "Could not find the correct provider above this X Widget"

我正在尝试在 urlTemplate 上创建动态地图路径,但是 Provider (a.k.a provider)抛出这个错误。 (请参考附图)。

在其他页面中,provider 工作正常,但在此页面除外。我想知道为什么在我的情况下使用 Provider.of(context) 不起作用的问题。

对此有什么想法吗?我整晚都在寻找这个,但没有任何运气。

import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong/latlong.dart';
import 'package:provider/provider.dart';
import 'package:user_location/user_location.dart';
import '../model/events.dart';

class MapBox extends StatefulWidget {
  final GlobalKey<ScaffoldState> scaffoldState = GlobalKey();
  @override
  _MapBoxState createState() => _MapBoxState();
}

class _MapBoxState extends State<MapBox> {
  MapController mapController = MapController();
  UserLocationOptions userLocationOptions;
  List<Marker> markers = [];

  @override
  Widget build(BuildContext context) {
    final events = Provider.of<Events>(context);
    final String maps = "${events.itmaps}";
    final String title = "${events.title}";
    userLocationOptions = UserLocationOptions(
      context: context,
      mapController: mapController,
      markers: markers,
    );
    return new Scaffold(
      appBar: new AppBar(title: new Text('title')),
      body: Stack(
        children: <Widget>[
          new FlutterMap(
            options: new MapOptions(
              center: new LatLng(46.185, 12.963),
              minZoom: 14.0,
              plugins: [
                // ADD THIS
                UserLocationPlugin(),
              ],
            ),
            layers: [
              new TileLayerOptions(
                urlTemplate: maps,
                additionalOptions: {
                  'accessToken':
                      '<XXX>',
                  'id': 'mapbox.mapbox-streets-v7'
                },
              ),
              MarkerLayerOptions(markers: markers),
              userLocationOptions,
            ],
            mapController: mapController,
          ),
        ],
      ),
    );
  }
}

这是我的 Events class:

class Events { 
   final String imagePath, title, itmaps;
   Events({this.imagePath, this.title, this.itmaps}); 
}

您可能想分享 "provided" 您的 Events 对象的 instance/value 的位置和方式。

显然,错误消息表明它正在寻找提供 Events.

的祖先,或者至少是 MapBox 的父窗口小部件

假设您的 Events 应该在您的应用程序中全局使用,您可以这样做:

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class Events {
  final String imagePath, title, itmaps;
  Events({this.imagePath, this.title, this.itmaps});
}

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // Creating an instance of events
  Events _declaredEvents = Events(
    imagePath: "https://somepath.com",
    title: "sometitle",
    itmaps: "somemaps",
  );

  @override
  Widget build(BuildContext context) {
    return Provider(
      create: (_) => _declaredEvents, // IMPORTANT: Create the object to be provided
      child: MaterialApp(
        home: FirstPage(),
      ),
    );
  }
}

class FirstPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // Based on the context of this page, look for the type `Events` that was provided by the top level widget/s
    Events _providedEvents = Provider.of<Events>(context);
    return Scaffold(
      body: Center(
        child: Text(_providedEvents.title), // Utilised the provided object here
      ),
    );
  }
}

希望对您有所帮助。