无法将 mobx 集成到简单的 flutter 应用程序中,该应用程序会在点击时增加计数器
Cant integrate mobx to simple flutter app that increments the counter on click
我是 flutter 新手,无法解决问题。
无法编译您的应用程序,因为无法建立其依赖项。
以下 Dart 文件:
/Users/anirudhsharma392/Desktop/flutter/testing/lib/counter/counter.飞镖
...在导入中引用以下库:
/Users/anirudhsharma392/Desktop/flutter/testing/lib/counter/counter.g.dart
不幸的是,该库似乎不存在于您的文件系统中。
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:mobx/mobx.dart';
part 'counter.g.dart';
class Counter = CounterBase with _$Counter;
abstract class CounterBase implements Store {
@observable
int value = 0;
@action
void increment() {
value++;
}
}
class CounterExample extends StatefulWidget {
const CounterExample({Key key}) : super(key: key);
@override
_CounterExampleState createState() => _CounterExampleState();
}
class _CounterExampleState extends State<CounterExample> {
final _counter = Counter();
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('Counter'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Observer(
builder: (_) => Text(
'${_counter.value}',
style: const TextStyle(fontSize: 20),
)),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _counter.increment,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
Mobx 使用代码生成来工作。您需要 运行 另一个命令来生成 Mobx 工作所需的文件
有两种方法可以生成这些文件:
flutter pub pub run build_runner build
flutter pub pub run build_runner watch
前者是一次性的一代。而后者会在需要更新时不断观察您的来源以更新生成的文件
请务必将 build_runner
添加到您的依赖项中:
dev_dependencies:
build_runner: ^1.1.2
我是 flutter 新手,无法解决问题。
无法编译您的应用程序,因为无法建立其依赖项。 以下 Dart 文件: /Users/anirudhsharma392/Desktop/flutter/testing/lib/counter/counter.飞镖 ...在导入中引用以下库: /Users/anirudhsharma392/Desktop/flutter/testing/lib/counter/counter.g.dart 不幸的是,该库似乎不存在于您的文件系统中。
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:mobx/mobx.dart';
part 'counter.g.dart';
class Counter = CounterBase with _$Counter;
abstract class CounterBase implements Store {
@observable
int value = 0;
@action
void increment() {
value++;
}
}
class CounterExample extends StatefulWidget {
const CounterExample({Key key}) : super(key: key);
@override
_CounterExampleState createState() => _CounterExampleState();
}
class _CounterExampleState extends State<CounterExample> {
final _counter = Counter();
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('Counter'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Observer(
builder: (_) => Text(
'${_counter.value}',
style: const TextStyle(fontSize: 20),
)),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _counter.increment,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
Mobx 使用代码生成来工作。您需要 运行 另一个命令来生成 Mobx 工作所需的文件
有两种方法可以生成这些文件:
flutter pub pub run build_runner build
flutter pub pub run build_runner watch
前者是一次性的一代。而后者会在需要更新时不断观察您的来源以更新生成的文件
请务必将 build_runner
添加到您的依赖项中:
dev_dependencies:
build_runner: ^1.1.2