Skip to content

Instantly share code, notes, and snippets.

@marcossevilla
Created June 17, 2023 05:51
Show Gist options
  • Save marcossevilla/70dd667ac9545f93a8234f14d308bd71 to your computer and use it in GitHub Desktop.
Save marcossevilla/70dd667ac9545f93a8234f14d308bd71 to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:translator/l10n/l10n.dart';
// To parse this JSON data, do
//
// final translation = translationFromJson(jsonString);
Translation translationFromJson(String str) {
return Translation.fromJson(
json.decode(str) as Map<String, dynamic>,
);
}
String translationToJson(Translation data) => json.encode(data.toJson());
// ignore: must_be_immutable
class Translation extends Equatable {
Translation({
required this.input,
required this.translated,
});
factory Translation.fromJson(Map<String, dynamic> json) {
return Translation(
input: json['input'] as String,
translated: Translated.fromJson(
json['translated'] as Map<String, dynamic>,
),
);
}
String input;
Translated translated;
Map<String, dynamic> toJson() => {
'input': input,
'translated': translated.toJson(),
};
@override
List<Object> get props => [input, translated];
}
// ignore: must_be_immutable
class Translated extends Equatable {
Translated({
required this.de,
required this.en,
required this.es,
required this.fr,
});
factory Translated.fromJson(Map<String, dynamic> json) => Translated(
de: json['de'] as String,
en: json['en'] as String,
es: json['es'] as String,
fr: json['fr'] as String,
);
String de;
String en;
String es;
String fr;
Map<String, dynamic> toJson() => {
'de': de,
'en': en,
'es': es,
'fr': fr,
};
@override
List<Object> get props => [de, en, es, fr];
}
class CounterPage extends StatelessWidget {
CounterPage({
super.key,
FirebaseFirestore? firestore,
}) : _firestore = firestore ?? FirebaseFirestore.instance;
final FirebaseFirestore _firestore;
@override
Widget build(BuildContext context) {
final l10n = context.l10n;
return Scaffold(
appBar: AppBar(title: Text(l10n.counterAppBarTitle)),
body: StreamBuilder(
stream: _firestore
.collection('translations')
.withConverter(
fromFirestore: (snapshots, _) => Translation.fromJson(
snapshots.data()!,
),
toFirestore: (translation, _) => translation.toJson(),
)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center(
child: Text('no data'),
);
}
if (snapshot.data == null) {
return const Center(
child: Text('null data'),
);
}
final data = snapshot.requireData;
return ListView.builder(
itemCount: data.size,
itemBuilder: (context, index) {
final translation = data.docs[index].data();
return ListTile(
title: Text(translation.input),
subtitle: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
for (final prop in translation.translated.props)
Text(prop.toString()),
],
),
);
},
);
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment