diff --git a/example/lib/main.dart b/example/lib/main.dart index 946941e82..08f012cbf 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,8 +1,13 @@ +import 'dart:io'; +import 'dart:typed_data'; + import 'package:flutter/material.dart'; import 'package:flutter_plugin_example/application_constants.dart'; import 'package:flutter_plugin_example/diet_plan.dart'; import 'package:flutter_stetho/flutter_stetho.dart'; import 'package:parse_server_sdk/parse_server_sdk.dart'; +import 'package:http/http.dart' as http; +import 'package:path_provider/path_provider.dart'; void main() { Stetho.initialize(); @@ -64,9 +69,52 @@ class _MyAppState extends State { } Future createItem() async { + //Create user Anonymous + ParseUser user = ParseUser('', '', ''); + final ParseResponse response = await user.loginAnonymous(); + + if (response.success) { + user = response.result as ParseUser; + } + + //Creates a temporary file in local storage to upload to Parse Server + //The file can be selected using ImagePicker + //File file = await ImagePicker.pickImage(source: ImageSource.gallery); + final File file = await _downloadFile( + 'https://i2.wp.com/blog.openshift.com/wp-content/uploads/parse-server-logo-1.png', + 'image.png'); + + final ParseFile parseFile = ParseFile(file, name: 'image.png'); + final ParseResponse fileResponse = await parseFile.save(); + + if (fileResponse.success) { + print('Upload with success'); + } else { + print('Upload with error'); + } + final ParseObject newObject = ParseObject('TestObjectForApi'); newObject.set('name', 'testItem'); newObject.set('age', 26); + newObject.set('price', 1.99); + newObject.set('found', true); + newObject.set>('listString', ['a', 'b', 'c', 'd']); + newObject.set>('listNumber', [0, 1]); + newObject.set('dateTest', DateTime.now()); + newObject.set('location', + ParseGeoPoint(latitude: 37.4230567, longitude: -122.0924609)); + + if (user != null) { + newObject.set('user', user); //save Pointer to user + newObject.set>( + 'users', [user, user]); //save array Pointer to users + } + + if (parseFile.url != null) { + newObject.set('fileImage', parseFile); //save ParseFile + newObject.set>( + 'fileImages', [parseFile, parseFile]); //save array ParseFile + } final ParseResponse apiResponse = await newObject.create(); @@ -129,8 +177,11 @@ class _MyAppState extends State { } Future query() async { + + final ParseUser user = await ParseUser.currentUser(); final QueryBuilder queryBuilder = QueryBuilder(ParseObject('TestObjectForApi')) + ..whereEqualTo('user', user.toPointer()) //query using Pointer ..whereLessThan(keyVarCreatedAt, DateTime.now()); final ParseResponse apiResponse = await queryBuilder.query(); @@ -259,4 +310,15 @@ class _MyAppState extends State { print('We have our configs.'); } } + + //Create a temporary file in local storage for upload in Parse + Future _downloadFile(String url, String filename) async { + final http.Client _client = http.Client(); + final http.Response req = await _client.get(Uri.parse(url)); + final Uint8List bytes = req.bodyBytes; + final String dir = (await getApplicationDocumentsDirectory()).path; + final File file = File('$dir/$filename'); + await file.writeAsBytes(bytes); + return file; + } } diff --git a/example/pubspec.yaml b/example/pubspec.yaml index ba2f6813c..54273a223 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -9,6 +9,9 @@ dependencies: # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^0.1.2 flutter_stetho: ^0.2.2 + path_provider: ^0.5.0+1 + http: any + dev_dependencies: parse_server_sdk: