Last active
January 27, 2020 05:30
-
-
Save nobuhito/2bb4928204c4fe8f74dfdc17a8000da7 to your computer and use it in GitHub Desktop.
型指定しないとFlutter自体のExtensionでエラーが発生
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
Hoge hoge = Hoge()..setDuration(Duration(hours: 5)); | |
return MaterialApp( | |
title: 'flutter_extension_sample', | |
home: Scaffold( | |
body: ChangeNotifierProvider.value( | |
value: hoge, | |
builder: (context, Hoge hoge, _) { // require Hoge | |
return Center( | |
child: Text(hoge.duration.toHour()), | |
); | |
}, | |
), | |
), | |
); | |
} | |
} | |
class Hoge extends ChangeNotifier { | |
Duration duration; | |
Hoge(); | |
Future<void> setDuration(Duration duration) async { | |
Future.delayed(Duration(seconds: 3), () { | |
this.duration = duration; | |
notifyListeners(); | |
}); | |
} | |
} | |
extension MyDurationX on Duration { | |
String toHour() { | |
return "${this.inHours}h"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: flutter_extension_sample | |
description: A new Flutter project. | |
version: 1.0.0+1 | |
environment: | |
sdk: ">=2.6.0 <3.0.0" | |
dependencies: | |
flutter: | |
sdk: flutter | |
cupertino_icons: ^0.1.2 | |
provider: | |
git: | |
url: https://github.com/rrousselGit/provider.git | |
ref: extensions | |
dev_dependencies: | |
flutter_test: | |
sdk: flutter | |
flutter: | |
uses-material-design: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment