質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Dart

Dartは、Googleによって開発されたJavaScriptの代替となることを目的に作られた、ウェブ向けのプログラミング言語である。

Q&A

0回答

1336閲覧

flutterでscoped modelで作成されたclassの中のメソッドをコールしたい

luckyclock

総合スコア73

Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Dart

Dartは、Googleによって開発されたJavaScriptの代替となることを目的に作られた、ウェブ向けのプログラミング言語である。

0グッド

0クリップ

投稿2018/11/21 11:10

b.dart内のonPressedの処理内でa.dart内Deviceクラスのtestメソッドを実行したいのですが、
どうすればよいのかわかりません。
DeviceクラスはBlueModelクラス内で宣言されています。

a.dart

dart

1class Device extends BluetoothDevice { 2 3 Device({this.connection, this.id, this.name}) 4 : super(id: id, name: name); 5 6 void test(){ 7 } 8} 9 10class BlueModel extends Model { 11 FlutterBlue _blue = FlutterBlue.instance; 12 Map<DeviceIdentifier, Device> _devices = {}; 13 Device currentDevice; 14 TabController tabController; 15 16 BlueModel({ this.tabController}) : assert(tabController != null); 17 18 bool _loading = false; 19 20 Map<DeviceIdentifier, Device> get devices => _devices; 21 22 static BlueModel of(BuildContext context) => 23 ScopedModel.of<BlueModel>(context); 24 25 bool get loading => _loading; 26 27 Future<void> scan() async { 28 _loading = true; 29 notifyListeners(); 30 31 var scannedResult = Map<DeviceIdentifier, Device>(); 32 33 await for (ScanResult result in _blue.scan(timeout: Duration(seconds: 2))) { 34 var dev = result.device; 35 if (dev.name.contains('ZEAL')) { 36 scannedResult[dev.id] = Device(id: dev.id, name: dev.name); 37 } 38 } 39 40 _devices.forEach((id, dev) { 41 if (scannedResult[id] == null && !_devices[id].connected) 42 _devices.remove(id); 43 }); 44 45 scannedResult.forEach((id, dev) { 46 if (_devices[id] == null) _devices[id] = dev; 47 }); 48 49 _loading = false; 50 notifyListeners(); 51 } 52 53 void connect(Device d) async { 54 d.currentState = BluetoothDeviceState.connecting; 55 notifyListeners(); 56 var deviceState = await d.state; 57 print('Device State: $deviceState'); 58 if (_devices[d.id].connection == null && 59 deviceState != BluetoothDeviceState.connected) { 60 _devices[d.id].connection = _blue.connect(d).listen((s) { 61 print('Current State: ${d.currentState}'); 62 if (s == BluetoothDeviceState.connected) { 63 d.initiate(notifyListeners); 64 } 65 }); 66 } else { 67 d.currentState = BluetoothDeviceState.connecting; 68 notifyListeners(); 69 await Future.delayed(Duration(milliseconds: 500)); 70 d.currentState = BluetoothDeviceState.disconnected; 71 notifyListeners(); 72 } 73 } 74 75 void disconect(Device d) { 76 d.dispose(); 77 if (d.id == currentDevice.id) currentDevice = null; 78 notifyListeners(); 79 } 80 81 void select(Device d) async { 82 print('Checking...'); 83 84 if (!d.connected) return; 85 86 if (currentDevice != null && d.id != currentDevice.id) { 87 print('Stop current device'); 88 currentDevice.stopReadWeight(); 89 currentDevice.stopListen(); 90 } 91 92 print('Checked!!!'); 93 94 if (!d.listening) { 95 print('Start listening'); 96 currentDevice = d; 97 currentDevice.startListen(notifyListeners); 98 currentDevice.startReadWeight(); 99 } 100 101 tabController.animateTo(1); 102 } 103}

b.dart

dart

1class BasicFunctions extends StatelessWidget { 2 3 Widget build(BuildContext context) { 4 return Wrap( 5 alignment: WrapAlignment.spaceAround, 6 runSpacing: 20.0, 7 children: <Widget>[ 8 ControlButton( 9 text: 'aaa', 10 onPressed: () { 11 //このonPressedの中でDeviceクラスのtestメソッドを実行したい 12 }, 13 ), 14 ], 15 ); 16 } 17}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問