Firebase사용법
import 'package:http/http.dart' as http;
final url = Uri.https('https를 제외한 firebase 주소', '저장하고싶은 이름(ex.shopping-list).json');
final response = await http.post(
url,
headers: {
'Content-Type': 'application/json',
},
body: json.encode(
{
'name': _enteredName,
'quantity': _enteredQuantity,
'category': _selectedCategory.title,
},
),
데이터를 가져올 때까지의 로딩을 보여주는 방법
var _isLoading = true;
setState(() {
_groceryItems = loadedItems;
_isLoading = false;
});
if (_isLoading) {
content = const Center(child: CircularProgressIndicator());
}
? : 구문을 이용해 사용자에게 더 좋은 경험을 선사하는 방법
onPressed: _isSending ? null : _saveItem
와 같이 구현.
동그라미 로딩 코드
CircularProgressIndicator()
Url이 잘못되었을 때 에러 메시지를 뜨게하는 법
firebase에 데이터 삭제할 때 ${item.id}로 삭제 가능.
void _removeItem(GroceryItem item) async {
final index = _groceryItems.indexOf(item);
setState(() {
_groceryItems.remove(item);
});
final url = Uri.https('flutter-prep-b61bd-default-rtdb.firebaseio.com',
'shopping-list/${item.id}.json');
final response = await http.delete(url);
if (response.statusCode >= 400) {
setState(() {
_groceryItems.insert(index, item);
});
const snackBar = SnackBar(
content: Text('Error occured! Please try later!'),
);
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}
Firebase에서 저장된 데이터가 없을 때 무한 로딩 대처법
if (response.body == 'null') {
setState(() {
_isLoading = false;
});
return;
}
+FutureBuilder 사용