Flutter Desktop Apps: Getting Started

Mar 28 2023 · Dart 2.19, Flutter 3.7, Android Studio 2021.3.1 or higher, Visual Studo Code 1.7.4 or higher

Part 1: Flutter Desktop Apps

11. Importing Data

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 10. Exporting Data Next episode: 12. Create the Desktop Icon

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Android Studio

Let’s test this out by deleting our todos and writing the import code.

final result = await FilePicker.platform.pickFiles(
  dialogTitle: 'Open file',
  allowedExtensions: ['todo'],
  type: FileType.custom

);
if (result == null || result.files.isEmpty) {
  return;
}
final path = result.files[0].path;
// 1
if (path != null) {
  // 2
  final file = File(path);
  // 3
  if (await file.exists()) {
    // 4
    final repository = ref.read(repositoryProvider);
    // 5
    file.readAsString().then((String contents) async {
      // 6
      final jsonArray = jsonDecode(contents) as List<dynamic>;
      // 7
      await Future.forEach(jsonArray, (value) async  {
        final map = value as Map<String, dynamic>;
        var list = TodoList.fromJson(map);
        // 8
        list = list.copyWith(id: -1);
        // 9
        final todoListId = await repository.addTodoList(list);
        // 10
        await Future.forEach(list.categories, (category) async {
          // 11
          category = category.copyWith(id: -1, todoList: todoListId);
          // 12
          final categoryId = await repository.addCategory(category);
          await Future.forEach(category.todos, (todo) async {
          	todo = todo.copyWith(id: -1, category: categoryId);
            await repository.addTodo(todo);
          });
        });
      });
    });
  }
}