Flutter Provider 值为空

Flutter Provider value is null

我试图通过提供程序将一个小部件的索引传递给另一个小部件,但提供程序上下文的值 returns 似乎是空的。

  @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 class 中调用提供程序,如下所示:

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

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

编辑:即使代码被缩减,它对于 post 来说似乎也太长了。我在 Gdrive

上上传了两个 classes 和 yaml

而不是:

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

你应该关注你的供应商:

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

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

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();
  }
}