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