The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
update flutter chat app blog to use supabase-flutter 1.0
dshukertjr988250627989
diff --git a/apps/www/_blog/2022-06-30-flutter-tutorial-building-a-chat-app.mdx b/apps/www/_blog/2022-06-30-flutter-tutorial-building-a-chat-app.mdxindex 322896dd24a..4b965fb176b 100644--- a/apps/www/_blog/2022-06-30-flutter-tutorial-building-a-chat-app.mdx+++ b/apps/www/_blog/2022-06-30-flutter-tutorial-building-a-chat-app.mdx@@ -374,13 +374,18 @@ class Message {### Step 5: Create register page with email, password and username-Now that we have defined a few handy constants, it is time to dive into creating pages. The first page we will create is the register page. This page will take an email address, password, and username within a form widget. The username will be the primary identifier when users search for other users within the app. Once a user performs registration, they will be taken to the chat page. Let's create a `lib/pages/register_page.dart` file and paste the following code.+Now that we have defined a few handy constants, it is time to dive into creating pages.+The first page we will create is the register page.+This page will take an email address, password, and username within a form widget.+The username will be the primary identifier when users search for other users within the app.+Once a user performs registration, they will be taken to the chat page. Let's create a `lib/pages/register_page.dart` file and paste the following code.```dart title="lib/pages/register.dart"import 'package:flutter/material.dart';import 'package:my_chat_app/pages/chat_page.dart';import 'package:my_chat_app/pages/login_page.dart';import 'package:my_chat_app/utils/constants.dart';+import 'package:supabase_flutter/supabase_flutter.dart';class RegisterPage extends StatefulWidget {const RegisterPage({Key? key, required this.isRegistering}) : super(key: key);@@ -414,15 +419,16 @@ class _RegisterPageState extends State<RegisterPage> {final email = _emailController.text;final password = _passwordController.text;final username = _usernameController.text;- final res = await supabase.auth- .signUp(email, password, userMetadata: {'username': username});- final error = res.error;- if (error != null) {+ try {+ await supabase.auth.signUp(+ email: email, password: password, data: {'username': username});+ Navigator.of(context)+ .pushAndRemoveUntil(ChatPage.route(), (route) => false);+ } on AuthException catch (error) {context.showErrorSnackBar(message: error.message);- return;+ } catch (error) {+ context.showErrorSnackBar(message: unexpectedErrorMessage);}- Navigator.of(context)- .pushAndRemoveUntil(ChatPage.route(), (route) => false);}@override@@ -505,7 +511,12 @@ class _RegisterPageState extends State<RegisterPage> {If you look at the validator function of the username field, you notice that we are enforcing the same regular expression check as what we defined in our table definition of profiles.-If you take a closer look at the `_signup()` method, you notice that the username is passed as a userMetadata. We will need to copy this username into our profiles table so that other users can find you. In order to do this, we will utilize a [Postgres function](https://supabase.com/docs/guides/database/functions) and Postgres trigger. Run the following SQL to create a Postgres function that will automatically run when a new user signs up to our application. Since we have set a unique constraint on the username column of our profiles table, the sign up will fail if a user chooses a username that is already taken.+If you take a closer look at the `_signup()` method, you notice that the username is passed as a `data`.+By passing `data` to your sign up method, you can populate the meta data field of your authenticated users.+We will need to copy this username into our profiles table so that other users can find you.+In order to do this, we will utilize a [Postgres function](https://supabase.com/docs/guides/database/functions) and Postgres trigger.+Run the following SQL to create a Postgres function that will automatically run when a new user signs up to our application.+Since we have set a unique constraint on the username column of our profiles table, the sign up will fail if a user chooses a username that is already taken.```sql-- Function to create a new row in profiles table upon signup@@ -526,18 +537,23 @@ create trigger on_auth_user_createdexecute function handle_new_user();```-Also, Supabase has email confirmation turned on by default, meaning that every time someone signs up, they have to click the confirmation link they receive in their email. This is ideal for a production app, but for our sample app, we can turn it off since we want to get up and running with building a functioning chat app. We will cover secure authentications using Supabase in later articles. Go to authentication → settings and turn off the switch of `Enable email confirmations`.+Also, Supabase has email confirmation turned on by default, meaning that every time someone signs up, they have to click the confirmation link they receive in their email.+This is ideal for a production app, but for our sample app, we can turn it off since we want to get up and running with building a functioning chat app.+We will cover secure authentications using Supabase in later articles.+Go to authentication → settings and turn off the switch of `Enable email confirmations`.### Step 6: Create login page-Login page will also be a simple page with an email and password field. Once they have signed in, the user will be taken to the rooms page.+Login page will also be a simple page with an email and password field.+Once they have signed in, the user will be taken to the rooms page.```dart title="lib/pages/login_page.dart"import 'package:flutter/material.dart';import 'package:my_chat_app/pages/chat_page.dart';import 'package:my_chat_app/utils/constants.dart';+import 'package:supabase_flutter/supabase_flutter.dart';class LoginPage extends StatefulWidget {const LoginPage({Key? key}) : super(key: key);@@ -559,16 +575,23 @@ class _LoginPageState extends State<LoginPage> {setState(() {_isLoading = true;});- final response = await supabase.auth.signIn(- email: _emailController.text,- password: _passwordController.text,- );- final error = response.error;- if (error != null) {+ try {+ await supabase.auth.signInWithPassword(+ email: _emailController.text,+ password: _passwordController.text,+ );+ Navigator.of(context)+ .pushAndRemoveUntil(ChatPage.route(), (route) => false);+ } on AuthException catch (error) {context.showErrorSnackBar(message: error.message);+ } catch (_) {+ context.showErrorSnackBar(message: unexpectedErrorMessage);+ }+ if (mounted) {+ setState(() {+ _isLoading = true;+ });}- Navigator.of(context)- .pushAndRemoveUntil(ChatPage.route(), (route) => false);}@override@@ -610,7 +633,12 @@ class _LoginPageState extends State<LoginPage> {### Step 7: Create a chat page to receive and send real time messages-Last, we create the Chat page. This page will load the messages in real time and display them to the users. Users will also be able to send messages to everyone else using the app. We are using the [stream()](https://supabase.com/docs/reference/dart/stream) method on Supabase SDK to load the messages in realtime. As those messages come in, we are lazily loading the profiles of each message’s sender. We will display the user icon as soon as their profile data is available.+Last, we create the Chat page.+This page will load the messages in real time and display them to the users.+Users will also be able to send messages to everyone else using the app.+We are using the [stream()](https://supabase.com/docs/reference/dart/stream) method on Supabase SDK to load the messages in realtime.+As those messages come in, we are lazily loading the profiles of each message’s sender.+We will display the user icon as soon as their profile data is available.```dart title="lib/pages/chat_page.dart"import 'dart:async';@@ -620,6 +648,7 @@ import 'package:flutter/material.dart';import 'package:my_chat_app/models/message.dart';import 'package:my_chat_app/models/profile.dart';import 'package:my_chat_app/utils/constants.dart';+import 'package:supabase_flutter/supabase_flutter.dart';import 'package:timeago/timeago.dart';/// Page to chat with someone.@@ -647,9 +676,8 @@ class _ChatPageState extends State<ChatPage> {final myUserId = supabase.auth.currentUser!.id;_messagesStream = supabase.from('messages')- .stream(['id'])+ .stream(primaryKey: ['id']).order('created_at')- .execute().map((maps) => maps.map((map) => Message.fromMap(map: map, myUserId: myUserId)).toList());@@ -660,19 +688,12 @@ class _ChatPageState extends State<ChatPage> {if (_profileCache[profileId] != null) {return;}- final res = await supabase- .from('profiles')- .select()- .match({'id': profileId})- .single()- .execute();- final data = res.data;- if (data != null) {- final profile = Profile.fromMap(data);- setState(() {- _profileCache[profileId] = profile;- });- }+ final data =+ await supabase.from('profiles').select().eq('id', profileId).single();+ final profile = Profile.fromMap(data);+ setState(() {+ _profileCache[profileId] = profile;+ });}@override@@ -787,14 +808,15 @@ class _MessageBarState extends State<_MessageBar> {return;}_textController.clear();-- final res = await supabase.from('messages').insert({- 'profile_id': myUserId,- 'content': text,- }).execute();- final error = res.error;- if (error != null) {+ try {+ await supabase.from('messages').insert({+ 'profile_id': myUserId,+ 'content': text,+ });+ } on PostgrestException catch (error) {context.showErrorSnackBar(message: error.message);+ } catch (_) {+ context.showErrorSnackBar(message: unexpectedErrorMessage);}}}@@ -864,6 +886,6 @@ If you have any questions please reach out via [Twitter](https://twitter.com/dsh## More Flutter Resources- [Flutter Supabase Quick Starter Guide](https://supabase.com/docs/guides/with-flutter)-- [Quickly prototype your app using DartPad](https://dartpad.dev/)+- [supabase-flutter v1.0 released](https://supabase.com/blog/supabase-flutter-sdk-v1-released)- [Open source map based Flutter app with CI/CD and scalable statemanagement example](https://github.com/dshukertjr/spot)- [Build a Flutter app with Very Good CLI and Supabase](https://verygood.ventures/blog/flutter-app-very-good-cli-supabase)diff --git a/apps/www/public/images/blog/flutter-chat/turn-off-email-confirmation.png b/apps/www/public/images/blog/flutter-chat/turn-off-email-confirmation.pngindex d70bafed691..ffffe991b1d 100644Binary files a/apps/www/public/images/blog/flutter-chat/turn-off-email-confirmation.png and b/apps/www/public/images/blog/flutter-chat/turn-off-email-confirmation.png differ