Flutter中的提供程序,错误为“在此X小部件上方找不到正确的提供程序”

时间:2020-03-02 03:16:15

标签: flutter provider

我正在尝试在urlTemplate上创建动态地图路径,但是 Provider(又称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,
          ),
        ],
      ),
    );
  }
}

enter image description here

这是我的Events班:

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

1 个答案:

答案 0 :(得分:1)

您可能想要共享在何处以及如何“提供” Events对象的实例/值。

很明显,错误消息指出它正在寻找提供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
      ),
    );
  }
}

希望这会有所帮助。