Skip to content

Instantly share code, notes, and snippets.

@ritshpatidar
Created October 6, 2019 05:22
Show Gist options
  • Save ritshpatidar/f31933b885670e468a8e9117e8314901 to your computer and use it in GitHub Desktop.
Save ritshpatidar/f31933b885670e468a8e9117e8314901 to your computer and use it in GitHub Desktop.
//Put this code in the class which extens State class
static const platform = const MethodChannel('samples.flutter.dev/filesChannel'); //used to connect with Java/Kotlin
List<dynamic> fileNames = <dynamic>[];
Future<void> _invokeNativeFun(int flag) async {
String s;
try {
if(flag == 0)
fileNames = await platform.invokeMethod('getNames'); //getNames function called from Kotlin/Java
} on PlatformException catch (e) {
s = "Failed: '${e.message}'."; //you can print it
}
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _invokeNativeFun(0),
builder: (context, snapshot){
if(snapshot.connectionState == ConnectionState.done){
return ListView.builder(
itemCount: fileNames.length,
itemBuilder: (BuildContext context, int index) => buildBody(context, index),
);
} else {
return Center(child: CircularProgressIndicator());
}
}
);
}
Widget buildBody(BuildContext context, int index){
return Text(fileNames[index],style:TextStyle(fontSize:20.0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment