pub.dev์์ ํจํค์ง ์ข์๊ฑฐ ์๋ ๋๋ฌ๋ณด๋ค๊ฐ ์๊ฐ๋ณด๋ค ์ธ๊ธฐ๊ฐ ์์ด๋ณด์ด๋ ์ฑ์ ๋ฐ๊ฒฌํ์ต๋๋ค.
ํจํค์ง ํ๋ฌ์ค..? ๊ฐ ๋ญ์ง.... ๋ผ๊ณ ์๊ฐํ๋ฉด์ ์ฌ์ฉ๋ฒ์ ์ฝ์ด๋ณด๋๋ฐ
๋ญ์ง..? ๋ ๋ชจ๋ฅด๊ฒ ๋๋ฐ
์ผ๋จ ์ฌ์ฉํด๋ณธ๊ฒฐ๊ณผ ํ์ฌ ํ๋ก์ ํธ์ ํจํค์ง๊ด๋ จ๋ ๋ด์ฉ์ ๊ฐ์ ธ์ฌ ์ ์๋ ํจํค์ง์ธ ๊ฒ ๊ฐ์ต๋๋ค.
์ฌ์ฉ๋ฒ์ ๊ฐ๋จํฉ๋๋ค.
pubspec.yaml ํ์ผ์ ํจํค์ง๋ฅผ ์ถ๊ฐํ๊ณ
// `PackageInfo.fromPlatform()`์ ์ ์ด ๋ผ์ธ์ ํธ์ถํ์ธ์
WidgetsFlutterBinding.ensureInitialized();
PackageInfo packageInfo = await PackageInfo.fromPlatform();
์ ๋๊ฐ๋ฅผ runApp() ์คํ์ ์ ์คํ์์ผ ์ฃผ๊ณ
String appName = packageInfo.appName;
String packageName = packageInfo.packageName;
String version = packageInfo.version;
String buildNumber = packageInfo.buildNumber;
์ด๋ ๊ฒ 4๊ฐ์ ๋ณ์์ ํจํค์ง ์ ๋ณด๋ฅผ ๋ฃ์ผ๋ฉด ๋ฉ๋๋ค.
๋ค๋ง ์ฌ๊ธฐ์ ๋ฌธ์ ์ ์ด,
PackageInfo packageInfo = await PackageInfo.fromPlatform();
๋ฅผ ์ธ์ ์ด๊ธฐํ ํ๋ ๋ถ๋ถ์ ๋๋ค.
์ ๊ฐ์๊ฒฝ์ฐ๋ ๊ทธ๋ฅ runApp()์ ์ ๋น๋๊ธฐ์ ์ผ๋ก ์ด๊ธฐํ๋ฅผ ์์ผ๋ฒ๋ ธ๋๋ฐ
void main() async {
// Be sure to add this line if `PackageInfo.fromPlatform()` is called before runApp()
WidgetsFlutterBinding.ensureInitialized();
PackageInfo packageInfo = await PackageInfo.fromPlatform();
runApp(
MaterialApp(
home: MyApp(
packageInfo: packageInfo,
),
),
);
}
๊ณต์์์๋ ์ด๋ฌํ ๋ฐฉ๋ฒ์ ์์์น ๋ชปํ ์ค๋ฅ๋ฅผ ์๊ธฐํ ์ ์๋ค๊ณ ๊ฐ๋ฅํ ์ง์ ํ๋ผ๊ณ ํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
๊ทธ๋์ ๊ฐ๋ฅํ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก๋ FutureBuilder๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ ์ ์์ต๋๋ค.
class _HomeScreenState extends State<HomeScreen> {
Future<PackageInfo> _loadPackageInfo() async {
return await PackageInfo.fromPlatform();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<PackageInfo>(
future: _loadPackageInfo(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return const Center(child: Text("Error loading package info"));
} else {
// Successfully loaded PackageInfo
final packageInfo = snapshot.data!;
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('appName : ${packageInfo.appName}'),
Text('packageName : ${packageInfo.packageName}'),
Text('version : ${packageInfo.version}'),
Text('buildNumber : ${packageInfo.buildNumber}'),
],
),
);
}
},
),
);
}
}
ํ์คํ FutureBuilder๋ฅผ ์ฌ์ฉํ๋ฉด ์์ ์ฑ์ด ๋ณด์ฅ๋๊ฒ ์ง๋ง ์ฝ๋์ ๊ฐ๋ ์ฑ ์ธก๋ฉด์์๋ ์์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
๊ณ ์ ํจํค์ง๋ช ์ ๋ถ๋ฌ์ค๊ธฐ ์ํด์ ์ฝ๋๊ฐ ๊ธธ์ด์ง๋ค๋๊ฒ์ ์ข์ ์ฝ๋๋ ์๋ ๋๋?
๋ฌดํผ ์ด๋ก์จ package_info_plus ์ฌ์ฉ๋ฒ์ ์์๋ณด์์ต๋๋ค.
์์ง ์ด ํจํค์ง๋ฅผ ์ด๋์ ์ฌ์ฉํด์ผ ํ ์ง ๊ฐ์ ์์ค์ง๋ง, ์ฃผ๋ก ๋ด ์ ๋ณดํ์ด์ง์ ์๋ "์ฑ ๋ฒ์ "์ ์ ๊ฒ ๋๋ค๋ฉด
์ ์ฉํ ๊ธฐ๋ฅ์ผ ๊ฒ ๊ฐ์ต๋๋ค.
์ด ํจํค์ง๋ฅผ ์์ด๋ค๋ฉด ํ๋์ฝ๋ฉํด์ผํ๋ฏ๋ก(ํ๋์ฝ๋ฉ์ ๋ฉ์ถฐ!)
์ ์ฒด ์ฝ๋
์ฝ๋ ๋ณด๊ธฐ
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
textTheme: const TextTheme(
bodyMedium: TextStyle(
fontSize: 14,
height: 2,
),
),
),
home: const HomeScreen(),
),
);
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
Future<PackageInfo> _loadPackageInfo() async {
return await PackageInfo.fromPlatform();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<PackageInfo>(
future: _loadPackageInfo(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return const Center(child: Text("Error loading package info"));
} else {
// Successfully loaded PackageInfo
final packageInfo = snapshot.data!;
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('appName : ${packageInfo.appName}'),
Text('packageName : ${packageInfo.packageName}'),
Text('version : ${packageInfo.version}'),
Text('buildNumber : ${packageInfo.buildNumber}'),
],
),
);
}
},
),
);
}
}