main.dart 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import 'dart:async';
  2. import 'package:printer/printer.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. void main() => runApp(MyApp());
  6. class MyApp extends StatefulWidget {
  7. @override
  8. _MyAppState createState() => _MyAppState();
  9. }
  10. class _MyAppState extends State<MyApp> {
  11. Printer p;
  12. String _platformVersion = 'Unknown';
  13. _MyAppState() {
  14. p = new Printer();
  15. }
  16. @override
  17. void initState() {
  18. super.initState();
  19. initPlatformState();
  20. }
  21. // Platform messages are asynchronous, so we initialize in an async method.
  22. Future<void> initPlatformState() async {
  23. String platformVersion;
  24. // Platform messages may fail, so we use a try/catch PlatformException.
  25. try {
  26. platformVersion = await Printer.platformVersion;
  27. } on PlatformException {
  28. platformVersion = 'Failed to get platform version.';
  29. }
  30. // If the widget was removed from the tree while the asynchronous platform
  31. // message was in flight, we want to discard the reply rather than calling
  32. // setState to update our non-existent appearance.
  33. if (!mounted) return;
  34. setState(() {
  35. _platformVersion = platformVersion;
  36. });
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. return MaterialApp(
  41. home: Scaffold(
  42. appBar: AppBar(
  43. title: const Text('Plugin example app'),
  44. ),
  45. body: Center(
  46. child: Text('Running on: $_platformVersion\n'),
  47. ),
  48. ),
  49. );
  50. }
  51. }