Last active
June 6, 2022 16:46
-
-
Save malibayram/1669705f91ce599f40bdc8be69f39860 to your computer and use it in GitHub Desktop.
get random users from api and save to firebase authentication and firestore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:whisperp/consts/index.dart'; | |
import 'package:whisperp/messaging_ui/constants.dart'; | |
import 'package:http/http.dart' as http; | |
class WelcomeScreen extends StatefulWidget { | |
const WelcomeScreen({super.key}); | |
@override | |
State<WelcomeScreen> createState() => _WelcomeScreenState(); | |
} | |
class _WelcomeScreenState extends State<WelcomeScreen> { | |
int _kaydedilenKullaniciSayisi = 0; | |
String _kaydedilenKullaniciIsimSoyisim = ""; | |
Future<void> _kullanicilariGetir() async { | |
final response = await http.get( | |
Uri.parse('https://randomuser.me/api/?results=200&nat=tr'), | |
); | |
if (response.statusCode == 200) { | |
final resMap = jsonDecode(response.body); | |
for (final userMap in (resMap['results'] as List).cast<Map>()) { | |
String displayName = | |
"${userMap['name']['first']} ${userMap['name']['last']}"; | |
String email = userMap['email']; | |
String photoURL = userMap['picture']['large']; | |
final userCredential = | |
await FirebaseAuth.instance.createUserWithEmailAndPassword( | |
email: email, | |
password: email.split('@').first, | |
); | |
if (userCredential.user != null) { | |
await userCredential.user!.updateDisplayName(displayName); | |
await userCredential.user!.updatePhotoURL(photoURL); | |
await FirebaseFirestore.instance | |
.collection('users') | |
.doc(userCredential.user!.uid) | |
.set({ | |
'displayName': displayName, | |
'email': email, | |
'emailVerified': false, | |
'photoURL': photoURL, | |
'registerationTime': FieldValue.serverTimestamp(), | |
}); | |
await Future.delayed(const Duration(seconds: 1)); | |
_kaydedilenKullaniciSayisi++; | |
_kaydedilenKullaniciIsimSoyisim = displayName; | |
debugPrint( | |
"$_kaydedilenKullaniciSayisi: _kaydedilenKullaniciIsimSoyisim"); | |
await FirebaseAuth.instance.signOut(); | |
setState(() {}); | |
} | |
} | |
} | |
} | |
@override | |
void initState() { | |
// _kullanicilariGetir(); | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: SafeArea( | |
child: Column( | |
children: [ | |
const Spacer(flex: 2), | |
Image.asset(AssetsPath.welcomeImage), | |
const Spacer(flex: 3), | |
Text( | |
"$_kaydedilenKullaniciSayisi\n$_kaydedilenKullaniciIsimSoyisim", | |
textAlign: TextAlign.center, | |
style: Theme.of(context) | |
.textTheme | |
.headline5! | |
.copyWith(fontWeight: FontWeight.bold), | |
), | |
Text( | |
"Welcome to our freedom \nmessaging app", | |
textAlign: TextAlign.center, | |
style: Theme.of(context) | |
.textTheme | |
.headline5! | |
.copyWith(fontWeight: FontWeight.bold), | |
), | |
const Spacer(), | |
Text( | |
"Freedom talk any person of your \nmother language.", | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
color: Theme.of(context) | |
.textTheme | |
.bodyText1! | |
.color! | |
.withOpacity(0.64), | |
), | |
), | |
const Spacer(flex: 3), | |
FittedBox( | |
child: TextButton( | |
onPressed: () { | |
_kullanicilariGetir(); | |
/* Hive.box(BoxNames.settings).put('is-skipped', true); | |
Navigator.pushNamed( | |
context, | |
RouteNames.signInOrSignUpScreen, | |
); */ | |
}, | |
child: Row( | |
children: [ | |
Text( | |
"Skip", | |
style: Theme.of(context).textTheme.bodyText1!.copyWith( | |
color: Theme.of(context) | |
.textTheme | |
.bodyText1! | |
.color! | |
.withOpacity(0.8), | |
), | |
), | |
const SizedBox(width: kDefaultPadding / 4), | |
Icon( | |
Icons.arrow_forward_ios, | |
size: 16, | |
color: Theme.of(context) | |
.textTheme | |
.bodyText1! | |
.color! | |
.withOpacity(0.8), | |
) | |
], | |
), | |
), | |
) | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment