Flutter Provider 值为 null

问题描述

我正在尝试通过提供程序将索引从小部件传递到另一个小部件,但提供程序上下文返回的值似乎为空。

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create:(ctx)=> ProvideIDX(),child: Scaffold(
        body: Container(
      child: Stack(
        children: <Widget>[
          Maposm(lieu),//,idcarousel),// Pass index to this class
          Align(
            alignment: Alignment.bottomCenter,child: CarouselSlider.builder(
                itemCount: lieu.length,itemBuilder: (BuildContext ctx,index) {
                  return Container(
                      color: Colors.yellow,child:Text(lieu[index].name));
                },options: CarouselOptions(
                  enableInfiniteScroll: true,scrollDirection: Axis.horizontal,onPageChanged: (index,reason) {
                    setState(() {
                      Index(id: index);
                      print(index);
                    });
                  },)),),],)));
  }
}

class Places {
  String name;
  LatLng coordxy;

  Places({this.name,this.coordxy});
}

class Index {
  int id;
  Index({this.id});
}

class ProvideIDX with ChangeNotifier{
  Index _selectedID;
  set selectedID(id){
    this._selectedID = id;
    notifyListeners();
  }
  get selectedID=>this._selectedID;

}

提供者在 Maposm 类中被调用,如下所示:

 @override
  Widget build(BuildContext context) {
    final _passIDX= Provider.of<ProvideIDX>(context);
    final  id = _passIDX.selectedID;
    print(id); // show null

我得到的结果是“null”,这当然不是我想使用的值^^

编辑:即使代码减少了,对于帖子来说似乎也太长了。我在 Gdrive

上传了两个类和 yaml

解决方法

代替:

final _passIDX= Provider.of<ProvideIDX>(context);

您应该注意您的提供商:

final _passIDX = context.watch<ProvideIDX>();

这是一个最小的工作示例:

enter image description here

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

void main() {
  runApp(
    ChangeNotifierProvider(
      create: (ctx) => ProvideIDX(),child: MaterialApp(
        debugShowCheckedModeBanner: false,title: 'Flutter Demo',home: HomePage(),),);
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: _Child(),floatingActionButton: FloatingActionButton(
        onPressed: () =>
            Provider.of<ProvideIDX>(context,listen: false).increment(),child: Icon(Icons.add),);
  }
}

class _Child extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final _passIDX = context.watch<ProvideIDX>();
    return Text(
      _passIDX.selectedID.toString(),style: TextStyle(fontSize: 96.0),);
  }
}

class Places {
  String name;
  Offset coordxy;

  Places({this.name,this.coordxy});
}

class Index {
  int id;
  Index({this.id});

  increment() => id++;

  toString() => id.toString();
}

class ProvideIDX with ChangeNotifier {
  Index _selectedID = Index(id: 0);
  get selectedID => _selectedID;
  set selectedID(id) {
    _selectedID = id;
    notifyListeners();
  }

  void increment() {
    _selectedID.increment();
    notifyListeners();
  }
}