【问题标题】:How to access other classes method in flutter (dart)?如何访问颤振(飞镖)中的其他类方法?
【发布时间】:2018-08-20 07:57:52
【问题描述】:

我在脚手架中使用了颤振图,并且在脚手架小部件类中有一个名为“showMyBottomSheet”的方法。当点击地图并打开底部工作表时用于执行此方法的地图。

但现在我已将这两个文件分开在单独的文件中,我无法从地图类访问脚手架状态小部件中的“showMyBottomSheet”方法。

我该怎么做?我不应该把它们分开吗?

我的主页 - home.dart

class MyHomePage extends StatefulWidget {
  static const String route = '/';

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const String route = '/';

  // Bottom sheet
  final _scaffoldKey = new GlobalKey<ScaffoldState>();

  dynamic showMyBottomSheet(LatLng latlang) {
    _scaffoldKey.currentState.showBottomSheet((context) {
      return new CustomBottomSheet();
    });
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: _scaffoldKey,
      drawer: buildDrawer(context, route),
      body: new Column(
        children: [
          new CustomMap(),
        ],
      ),
    );
  }
}

地图小部件 - flutterMap.dart

class Cuenter code herestomMap extends StatefulWidget {
  @override
  _CustomMapState createState() => new _CustomMapState();
}

class _CustomMapState extends State<CustomMap> {
  var markers = <Marker>[];

  @override
  Widget build(BuildContext context) {
    return new Flexible(
      child: new FlutterMap(
        options: new MapOptions(
            center: new LatLng(51.25, 30.5),
            zoom: 15.0,
            onTap: // I want to call showMyBottomSheet method (from home page) here
        ),
        layers: [
          new TileLayerOptions(
              urlTemplate:
              "https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png",
              subdomains: ['a', 'b', 'c']),
        ],
      ),
    );
  }
}

【问题讨论】:

  • 如果以下答案不起作用,请更新问题并指出您要调用该方法的位置。
  • 问题已更新。我删除了下划线并在我想调用该方法的地方添加了注释。
  • 查看答案
  • 成功了吗...?
  • 点击地图时出现 NoSuchMethodError

标签: class oop dart flutter


【解决方案1】:

首先,请注意,任何以下划线开头的方法在 Dart 中都将被视为私有方法。 look here.

在这种情况下,您应该将该方法传递给子小部件并稍后调用它。检查这个例子:

class MyHomePage extends StatefulWidget {
  static const String route = '/';

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const String route = '/';

  // Bottom sheet
  final _scaffoldKey = new GlobalKey<ScaffoldState>();

  dynamic showMyBottomSheet(LatLng latlang) {
    _scaffoldKey.currentState.showBottomSheet((context) {
      return new CustomBottomSheet();
    });
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: _scaffoldKey,
      drawer: buildDrawer(context, route),
      body: new Column(
        children: [
          new CustomMap(onMapTap: showMyBottomSheet),
        ],
      ),
    );
  }
}

在flutterMap.dart中:

class CustomMap extends StatefulWidget {
  Function onMapTap;
  CustomMap({this.onMapTap});

  @override
  _CustomMapState createState() => new _CustomMapState();
}

class _CustomMapState extends State<CustomMap> {
  var markers = <Marker>[];

  @override
  Widget build(BuildContext context) {
    return new Flexible(
      child: new FlutterMap(
        options: new MapOptions(
            center: new LatLng(51.25, 30.5),
            zoom: 15.0,
            onTap: (){
              widget.onMapTap(LatLng(34.12312,56.1323));
            }
        ),
        layers: [
          new TileLayerOptions(
              urlTemplate:
              "https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png",
              subdomains: ['a', 'b', 'c']),
        ],
      ),
    );
  }
}

【讨论】:

  • 将 onTab 函数更改为:‌(LatLng latlang) { widget.onMapTap(latlang); }
  • 但是如果 CustomMap 在不同的 Dart 文件中并且使用 pushNamed() 使用路由推送到导航器怎么办?怎么还能调用MyHomePage的方法呢?
猜你喜欢
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-22
  • 2022-06-20
相关资源
最近更新 更多