'How To Create Supabase Table In Flutter?
I want to make a chat application, where every user contains a table.. for that when new users register in the app. I have to create the table in supabase for himself/herself manually from flutter code only. not from supabase UI or external Postgres client.
Solution 1:[1]
In most applications it's usually not a good idea to create a new table for each user - but if you must do it this way you can use https://pub.dev/packages/postgres to connect to the supabase postgresql instance directly using the postgres query string (found in database > settings). You can then write queries to set up tables in SQL from within flutter.
Make sure you don't expose the postgres connection string to the user, and that you sanitize any strings that are used to build the sql queries
Solution 2:[2]
The problems with this code are how it stores the result of the distance calculation and how it uses sprintf to format the message you want to transmit.
First, you defined distance as an int, which really isn't going to work with this line:
distance= duration*0.034/2;
You're doing floating point math. If you store the result in an int, you'll lose any fractional value of the result. If the result was less than 1, it would end up being 0.
You also declared it to be a global variable but there appears to be no need for it to be global. It's better to limit its scope by only making it available where it's needed. In this case it looks like you can move both distance and duration inside loop(), so you'd remove their definitions before setup() and write them inside loop():
void loop() {
long duration;
float distance;
Your sprintf() code currently looks like this:
char msgDistance;
msgDistance = distance;
sprintf(msgDistance,"A distância lida é: ", distance);
There are several problems with this:
sprintf()needs to take a character array pointer as its first argument. Instead, you're passing it a single character variable. This makes no sense and is generating the compiler error you're seeing.sprintf()needs a format string to tell it what to print (written as%XwhereXis a character indicating the type of data being printed. You included some text but no format string.sprintf()can very easily overflow thechararray it's printing into; you should usesnprintf()instead
To solve the first problem, we need msgDistance to be an array of characters: char [].
Your code needs to look more like this:
char msgDistance[32];
snprintf(msgDistance, sizeof(msgDistance), "A distância lida é: %0.2f", distance);
The changes are:
msgDistanceis now a character array thatsnprintf()can use- deleted the unnecessary assign to
msgDistance, which would cause an error now that it's achararray snprintf()is the safer form ofsprintf()- it takes the length of the character array as an argument so that it can't accidentally overflow the arraysizeof()is a built-in function which returns the size of a variable at compile-time; it's an easy way to use the size of the array here but beware it may not do what you expect if you don't really understand C/C++ arrays and pointers and strings%0.2fin thesnprintf()format string - this tellssnprintf()to print afloatvariable (that's thef), with a leading zero if needed (that's the0part and two decimal places of precision (that's the2part - if you wanted one decimal place you'd write it as%0.1f)
You can learn more about printf() format strings using a search engine; there are many good tutorials and references for them available online.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Awalias |
| Solution 2 | romkey |
