nigeria_states_geography 1.0.0
nigeria_states_geography: ^1.0.0 copied to clipboard
A Flutter package that provides a list of Nigeria states and their capitals
example/nigeria_states_geography_example.dart
import 'package:flutter/material.dart';
import 'package:nigeria_states_geography/nigeria_states_geography.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<StatesAndCapitalModel> _states = [];
List<String> _capitals = [];
@override
void initState() {
super.initState();
_loadStatesAndCapital();
_loadAllCapitals();
}
/// a void function that loads the static method in [NigeriaGeography] class. the method [getAllStatesAndCapital] returns a list of model created[StatesAndCapitalModel]. [_states] an empty list that [_loadStatesAndCapital] appends the items returned by the method [getAllStatesAndCapital] to.
Future<void> _loadStatesAndCapital() async {
try {
final states = await NigeriaGeography.getAllStatesAndCapitals();
setState(() {
_states = states;
});
} catch (e) {
/// Handle the error (e.g., show a snackbar to the user)
print('Error loading states: $e');
SnackBar(
content: Text(
e.toString(),
),
);
}
}
/// a void function that loads the static method in [NigeriaGeography] class. the method [getAllCapitals] returns a list of Strings. [_capitals] an empty list that [_loadAllCapitals] appends the items returned by the method [getAllCapitals] to.
Future<void> _loadAllCapitals() async {
try {
final capitalList = await NigeriaGeography.getAllCapitals();
setState(() {
_capitals = capitalList;
});
} catch (e) {
/// Handle the error (e.g., show a snackbar to the user)
print('Error loading states: $e');
SnackBar(
content: Text(
e.toString(),
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Nigerian States'),
),
body: Column(
children: [
ListView.builder(
itemCount: _capitals.length,
itemBuilder: (context, index) {
// This variable holds the index of each String[capital]. The .dot builder method from Listview class now helps to iterate. the _capital List using the index
final capitalList = _capitals[index];
return ListTile(
title: Text(capitalList),
);
},
),
ListView.builder(
itemCount: _states.length,
itemBuilder: (context, index) {
final stateList = _states[index];
return ListTile(
title: Text(stateList.state),
subtitle: Text(stateList.capital),
);
},
),
],
),
);
}
}