Android studio Lost connection to device - flutter

I have an issue where i am running this code and getting all different combinations of the number without repeating.
It is put in a for loop where I have a list of numbers.
If the list is only of just 1 number, it seems to be alright. However when I have multiple numbers in the list, Android studio loses connection to my device.
Is it because my app is doing too much? If not how do I fix it?
List<String> rollNumberGenerator(String num) {
List numberToBeRolled = num.split('');
List<String> generatedRollList = [];
String zero = numberToBeRolled[0];
String one = numberToBeRolled[1];
String two = numberToBeRolled[2];
String three = numberToBeRolled[3];
String rollNumber1 = '$zero$one$two$three';
String rollNumber2 = '$zero$one$three$two';
String rollNumber3 = '$zero$three$one$two';
String rollNumber4 = '$three$zero$one$two';
String rollNumber5 = '$three$zero$two$one';
String rollNumber6 = '$zero$three$two$one';
String rollNumber7 = '$zero$two$three$one';
String rollNumber8 = '$zero$two$one$three';
String rollNumber9 = '$two$zero$one$three';
String rollNumber10 = '$two$zero$three$one';
String rollNumber11 = '$two$three$zero$one';
String rollNumber12 = '$three$two$zero$one';
String rollNumber13 = '$three$two$one$zero';
String rollNumber14 = '$two$three$one$zero';
String rollNumber15 = '$two$one$three$zero';
String rollNumber16 = '$two$one$zero$three';
String rollNumber17 = '$one$two$zero$three';
String rollNumber18 = '$one$two$three$zero';
String rollNumber19 = '$one$three$two$zero';
String rollNumber20 = '$three$one$two$zero';
String rollNumber21 = '$three$one$zero$two';
String rollNumber22 = '$one$three$zero$two';
String rollNumber23 = '$one$zero$three$two';
String rollNumber24 = '$one$zero$two$three';
generatedRollList.add(rollNumber1);
generatedRollList.add(rollNumber2);
generatedRollList.add(rollNumber3);
generatedRollList.add(rollNumber4);
generatedRollList.add(rollNumber5);
generatedRollList.add(rollNumber6);
generatedRollList.add(rollNumber7);
generatedRollList.add(rollNumber8);
generatedRollList.add(rollNumber9);
generatedRollList.add(rollNumber10);
generatedRollList.add(rollNumber11);
generatedRollList.add(rollNumber12);
generatedRollList.add(rollNumber13);
generatedRollList.add(rollNumber14);
generatedRollList.add(rollNumber15);
generatedRollList.add(rollNumber16);
generatedRollList.add(rollNumber17);
generatedRollList.add(rollNumber18);
generatedRollList.add(rollNumber19);
generatedRollList.add(rollNumber20);
generatedRollList.add(rollNumber21);
generatedRollList.add(rollNumber22);
generatedRollList.add(rollNumber23);
generatedRollList.add(rollNumber24);
List<String> validGeneratedRollList = [];
for (var numbers in generatedRollList) {
bool present = false;
present = validGeneratedRollList.contains(numbers);
if (present == false) {
validGeneratedRollList.add(numbers);
}
}
return validGeneratedRollList;
}
Thanks in advance for anyone that can help!

Related

Flutter_contacts update contacts function

I'm quite new to flutter/dart and working in Flutterflow, where trying to create a custom function that takes as input data about the contact in the app (name, surname, cell number, email, photo) and then updates/creates this contact in the user's phone using the next logic:
if a user has a contact with the same name (displayName), then it updates this contact
if a user has a contact with the same phone number, then it updates this contact
if there is no match based on two parameters before, then new contact is created
So far, I have only managed to create a function that checks displayName match, updates contact if there is a match, and if there is no match, it creates a new contact. But I don't know how to do the cell number match/update part (it doesn't work in my code).
The way I'm doing the search for the right contact to update is through the search of contact ID using lists of contacts' names and contacts' IDs to index the right ID when I found the necessary name. It's probably a very ugly and inefficient way to do it, but I don't know the other way.
Will be super happy if someone could give advice on the contact search/update part and overall code optimization (cause I think my approach is long and inefficient). Thank you!
My code is below:
import 'package:flutter_contacts/flutter_contacts.dart';
import 'dart:typed_data';
import 'package:flutter/services.dart';
Future updateContactOnPhone(
String name,
String surname,
String cellnumber,
String email,
String photo,
) async {
String searchingParameterName = name + " " + surname;
String searchingParameterCellphone = cellnumber;
List<String> phoneContactsIDs = [];
List<String> phoneContactsNames = [];
List<String> phoneContactsNumbers = [];
Uint8List bytes = (await NetworkAssetBundle(Uri.parse(photo)).load(photo))
.buffer
.asUint8List();
if (await FlutterContacts.requestPermission()) {
List<dynamic> contacts = await FlutterContacts.getContacts();
contacts.forEach((contact) {phoneContactsIDs.add(contact.id);});
contacts.forEach((contact) {phoneContactsNames.add(contact.displayName);});
contacts.forEach((contact) {if (contact.phones != null) {
phoneContactsNumbers.add(contact.phones.first);}
{phoneContactsNumbers.add("");}});
if (phoneContactsNames.contains(searchingParameterName)) {
int index = phoneContactsNames.indexOf(searchingParameterName);
String contactID = phoneContactsIDs.elementAt(index);
dynamic contact = await FlutterContacts.getContact(contactID);
contact.name.first = name;
contact.name.last = surname;
contact.phones = [Phone(cellnumber)];
contact.emails = [Email(email)];
await contact.update();
} else if (phoneContactsNumbers.contains(searchingParameterCellphone)) {
int index = phoneContactsNumbers.indexOf(searchingParameterCellphone);
String contactID = phoneContactsIDs.elementAt(index);
dynamic contact = await FlutterContacts.getContact(contactID);
contact.name.first = name;
contact.name.last = surname;
contact.phones = [Phone(cellnumber)];
contact.emails = [Email(email)];
await contact.update();
} else {
final newContact = Contact()
..name.first = name
..name.last = surname
..phones = [Phone(cellnumber)]
..emails = [Email(email)]
..photo = bytes;
await newContact.insert();
}}}
I tried various combinations of the code and searched for similar examples on forums, but nothing helped.
This is the code I wrote that worked for me. Hope it'll help someone.
import 'package:flutter_contacts/flutter_contacts.dart';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:collection/collection.dart';
Future updateCreateContact(
String name,
String surname,
String cellnumber,
String email,
String photo,
) async {
String searchingName = name + " " + surname;
Uint8List bytes = (await NetworkAssetBundle(Uri.parse(photo)).load(photo))
.buffer
.asUint8List();
if (await FlutterContacts.requestPermission()) {
List<Contact> contacts = await FlutterContacts.getContacts(
withProperties: true, withPhoto: true);
Contact? contact = contacts.firstWhereOrNull((c) =>
c.displayName == searchingName ||
c.phones.toString().replaceAll(RegExp(r"\D"), "") == cellnumber);
if (contact != null) {
contact.name.first = name;
contact.name.last = surname;
contact.phones = [Phone(cellnumber)];
contact.emails = [Email(email)];
contact.photo = bytes;
await contact.update();
} else {
final newContact = Contact()
..name.first = name
..name.last = surname
..phones = [Phone(cellnumber)]
..emails = [Email(email)]
..photo = bytes;
await newContact.insert();
}
}
}

How to extract specific string from whole string?

I have following strings :
String? hello = "(1.2,1.5 | 5)"
String? hi = "(2.3,3.2 | 9)"
Now I want to get
var newhello1 = 1.2,1.5
var newhello2 = 5
and
var newhi1 = 2.3,3.2
var newhi2 = 9
How to extract those text from that entire strings?
You can use the indexOf function combined with the substring to get the substrings as follows
var newhello1 = hello.substring(hello.indexOf('(') + 1, hello.indexOf('|')).trim(); //Use Trim() to get rid of any extra spaces
var newhello2 = hello.substring(hello.indexOf('|') + 1,hello.indexOf(')')).trim();
print(newhello1); //1.2,1.5
print(newhello2); //5
List<String> myformatter(String? data) {
if (data == null) return [];
List<String> ls = data.split("|");
for (int i = 0; i < ls.length; i++) {
ls[i] = ls[i].replaceAll("(", "").replaceAll(")", "").trim();
}
return ls;
}
main() {
String? hello = "(1.2,1.5 | 5)";
String? hi = "(2.3,3.2 | 9)";
final helloX = myformatter(hello);
print(helloX[0]); //1.2,1.5
print(helloX[1]); //5
final hiX = myformatter(hi);
print(hiX[0]); //2.3,3.2
print(hiX[1]); //9
}

How to split a string in Flutter while also including delimiters

+37.4054-122.0999/
The above coordinates are my output. I want to split the string in such a way that it shows +37.4054 and -122.0999 as substrings which includes the + and - signs.
You can do
string.split(RegExp('(?=[+-])'));
Example:
var string = '+37.4054-122.0999';
var string2 = '-37.4054+122.0999';
var string3 = '+37.4054+122.0999';
var string4 = '-37.4054-122.0999';
var a = string.split(RegExp('(?=[+-])'));
var b = string2.split(RegExp('(?=[+-])'));
var c = string3.split(RegExp('(?=[+-])'));
var d = string4.split(RegExp('(?=[+-])'));
print(a);
print(b);
print(c);
print(d);
Output:
[+37.4054, -122.0999]
[-37.4054, +122.0999]
[+37.4054, +122.0999]
[-37.4054, -122.0999]
Try use split method:
const string = '+37.4054-122.0999';
final splitted = string.split('-');
print(splitted); //['+37.4054', '122.0999'];
print(splitted[0]); //+37.4054;
print('-' + splitted[1]); //-122.0999;
Try this:
void main() {
final String coord = '+37.4054-122.0999';
final int plusIndex = coord.indexOf('+');
final int minusIndex = coord.indexOf('-');
final plus = coord.substring(plusIndex, minusIndex);
final minus = coord.substring(minusIndex, );
print('plus: $plus, minus: $minus');
}

Flutter search 'keywords' inside a String?

I want to search for a particular keyword in a String. For example, there is a string 'rajasthan' now if I want to search using 'rj', it will not detect anything, but if I search using 'raj' then it will. So what to do in this case?
My current way of searching:
String raj = 'rajasthan';
bool searchRaj = raj.contains('raj');
bool searchRJ = raj.contains('rj');
print('raj contains $searchRaj');
print('rj contains $searchRJ');
Dartpad:
https://dartpad.dev/?id=40472ce8cd82bd87ba916ea2f3e7eff9
Here you can find the example of how to achieve this.
void main() {
String raj = 'rajasthan';
String search1 = 'raj';
String search2 = 'rj';
var searchList = raj.split("").toSet();
var searchRAJ = search1.split("").toSet();
var searchRJ = search2.split("").toSet();
bool result1 = searchList.containsAll(searchRAJ);
bool result2 = searchList.containsAll(searchRJ);
print('raj contains $result1');
print('rj contains $result2');
}

How to format to json with difference between comma of data/key and comma from text?

I am trying to improve this code so that it can handle a specific case.
Currently it works, unless the user adds a text with a comma
Here is my input who work (look only "note" key/value)
Input_OK = 2020-11-25,note:my text,2020-11-25,today:2020-11-25,2020-09-14,start:2020-09-14
In this case : my text is ok because there is no comma
Input_NOK = 2020-11-25,note:my text, doesn't work,2020-11-25,today:2020-11-25,2020-09-14,start:2020-09-14
In this case : my text, doesn't work is not ok because there is comma
With this specific input 2020-11-25,note:my text, work now,2020-11-25,today:2020-11-25,2020-09-14,start:2020-09-14
I try to have this output
[{"release_date":"2020-11-25","today":"2020-11-25","note0":"my text, work now"},{"release_date":"2020-09-14","start":"2020-09-14"}]
Here is my current code
// before this input I add string to a list<String> for each date like that [2020-11-25,note0:test, 2020-11-24,my text, with comma, 2020-11-15,today:2020-11-15, 2020-09-14,start:2020-09-14]
//After I remove space and [ ]
// myinput 2020-11-25,today:2020-11-25,2020-11-25,note0:my text, with comma,2020-09-14,start:2020-09-14
var inputItarable = myinput.toString().split(',').where((s) => s.isNotEmpty);
print("inputItarable ${inputItarable} ");
//inputItarable [2020-11-25, today:2020-11-25, 2020-11-25, note0:my text, with comma, 2020-09-14, start:2020-09-14]
var i = inputItarable.iterator;
var tmp = {};
while (i.moveNext()) {
var key = i.current; i.moveNext();
var value = i.current.split(':');
(tmp[key] ??= []).add(value);
}
var output1 = tmp.keys.map((key) {
var map = {}; map['release_date'] = key;
tmp[key].forEach((e) => map[e[0]] = e[1]);
return map;
}).toList();
var output2=json.encode(output1);
print("output2 $output2 ");
// output2 [{"release_date":"2020-11-25","today":"2020-11-25","note0":"my text, with comma"},{"release_date":"2020-09-14","start":"2020-09-14"}]
[Edit] I have a spécific case, where user back ligne, and have an input like that
myinput 2020-11-25,today:2020-11-25,2020-11-25,note0:my text,
with comma,2020-09-14,start:2020-09-14
in this example I don't know how to replace the back ligne between my text, and with comma by my text,\nwith comma
Please check the code below or you may directly run it on Dartpad at https://dartpad.dev/1404509cc0b427b1f31705448b5edba3
I have written a sanitize function. What the sanitize function does is it sanitizes the text between the possibleStart and possibleEnd. Meaning it replaces all the commas in user input text with §. To do this it assumes that the user input starts with ,note: or ,note0: and ends with ,2020- or ,2021-. This sanitized string is passed to your code and in the end § is replaced with ",". Let me know if you have any questions.
import 'dart:convert';
String sanitize(
String input, List<String> possibleStart, List<String> possibleEnd) {
final String start = possibleStart.join("|");
final String end = possibleEnd.join("|");
final RegExp exp = RegExp("(?<=$start)(.*?)(?=$end)");
final Iterable<Match> matches = exp.allMatches(input);
matches.forEach((match) {
input =
input.replaceFirst(match.group(0), match.group(0).replaceAll(",", "§"));
return true;
});
return input;
}
void main() {
String myinput =
"2020-11-25,today:2020-11-25,2020-11-25,note0:my text, with comma,2020-09-14,start:2020-09-14";
myinput = sanitize(myinput, [",note:", "note\\d:"], [",20\\d\\d-"]);
var inputItarable = myinput.toString().split(',').where((s) => s.isNotEmpty);
print("inputItarable ${inputItarable} ");
//inputItarable [2020-11-25, today:2020-11-25, 2020-11-25, note0:my text, with comma, 2020-09-14, start:2020-09-14]
var i = inputItarable.iterator;
var tmp = {};
while (i.moveNext()) {
var key = i.current;
i.moveNext();
var value = i.current.split(':');
(tmp[key] ??= []).add(value);
}
var output1 = tmp.keys.map((key) {
var map = {};
map['release_date'] = key;
tmp[key].forEach((e) => map[e[0]] = e[1]);
return map;
}).toList();
var output2 = json.encode(output1).replaceAll("§", ",");
print("output2 $output2 ");
}