how to implement string list from _ListItem to assetPath? - string

i've been working with wallpaper app.
i manage to call the path list for wallpaper,
i can make this work on image card list but i can't manage to do it work with assetPath for WallpaperManager
Please help, is there any way to do it?
Future<void> setWallpaperFromAsset() async {
setState(() {
_wallpaperAsset = "Loading";
});
String result;
String assetPath = ('Load List here');

You can add a parameter assetPath to setWallpaperFromAsset
Future<void> setWallpaperFromAsset(String assetPath) async {
setState(() {
_wallpaperAsset = "Loading";
});
String result;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
result = await WallpaperManager.setWallpaperFromAsset(
assetPath, WallpaperManager.HOME_SCREEN);
} on PlatformException {
result = 'Failed to get wallpaper.';
}
}
And you register the onPressed callback with:
RaisedButton.icon(
onPressed: () => setWallpaperFromAsset(item),
icon: Icon (Icons.wallpaper, size: 20),
label: Text('homescreen')),
)

Related

Value returning from an async function is not the actual value

I'm calling an async function that has the file path as a parameter and reads and displays the content in the file.
this is the place where I'm invoking the function.
this is the function.
After reading the contents in the file, the data is printed in the console.
But when I try to use the same value to display in the emulator I'm getting error.
Why actual string value is not returned from the function??
error.
readContent is a future method, you need to wait to complete the fetching.
For future method, try using FutureBuilder.
late Future future = readContent();
#override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text("${snapshot.data}");
}
return CircularProgressIndicator();
},
),
);
}
Find more about using FutureBuilder.
An async function in dart can only return a Future.
Your readContent function signature doesn't declare a return value:
readContent(String path) async {
var result = await File(path).readAsString();
print(result);
return result
}
If you explicitly declare the return value as String you'll get a compilation error. Async methods can only return a future:
Future<String> readContent(String path) async {
var result = await File(path).readAsString();
print(result);
return result
}
The await keyword allows you to access the value returned by a Future, it is not the same as calling the function synchronously.
The only reason you need the await keyword in your code is because you're printing the value to console when the Future completes. You could do this instead, which is the same function without printing to console and without async await.
readContent(String path) {
return File(path).readAsString();
}
To address the issue described in your question, you might call readAsString synchronously:
readContent(String path) {
return File(path).readAsStringSync();
}
Or use await on the calling side
var val = await readContent(path);
Or
var txt = "";
readContent(path).then((value) {
txt = value;
setState(() {});
});
Or use the FutureBuilder widget

IPageRetriever not working with Ajax calls

I have the following API call to retrieve page data
List<VillageNewsItem> newsList = pageRetriever.RetrieveAsync<VillageNewsItem>(
query => query
.Path("/Home/Village-News", PathTypeEnum.Children)
.Published(true)
.OnSite(SiteContext.CurrentSiteName)
.OrderByDescending(x => x.DocumentCreatedWhen)
)?.Result?.ToList();
It works fine and return 2 records if I run the query on page load. Inside Index action of the controller.
public VillageNewsListController(IPageDataContextRetriever dataRetriever, VillageNewsListRepository villageNewsListRepository,
IPageRetriever pagesRetriever, IPageDataContextRetriever pageDataContextRetriever, IPageUrlRetriever pageUrlRetriever)
{
this._dataRetriever = dataRetriever;
this._villageNewsListRepository = villageNewsListRepository;
this._pagesRetriever = pagesRetriever;
this.pageDataContextRetriever = pageDataContextRetriever;
this.pageUrlRetriever = pageUrlRetriever;
}
public async Task<ActionResult> Index(CancellationToken cancellationToken)
{
try
{
List<VillageNewsItem> newsList = pagesRetriever.RetrieveAsync<VillageNewsItem>(
query => query
.Path("/Home/Village-News", PathTypeEnum.Children)
.Published(true)
.OnSite(SiteContext.CurrentSiteName)
.OrderByDescending(x => x.DocumentCreatedWhen)
)?.Result?.ToList();
newsItems.VillageNewsItems = newsList;
return View(newsItems);
}
catch (Exception ex)
{
ErrorHandler.EventLog.LogError(ex.Source, ex.Message, ex.StackTrace);
return RedirectToAction("ErrorPage", "Error");
}
}
However, if I try to make the same API call via a client side AJAX call, it doesn't work and return 0 records. Why it's not working with Ajax calls?
Ajax call
function loadMoreNews() {
$.ajax({
url: '/VillageNewsList/VillageNewsItemList',
//data: { "term": request.term },
type: "POST",
success: function (data) {
response($.map(data,
function (item) {
console.log(data);
}));
},
error: function (response) {
//alert(response.responseText);
},
failure: function (response) {
// alert(response.responseText);
}
});
}
Server side method.
[HttpPost]
[Route("VillageNewsList/VillageNewsItemList")]
public VillageNewsListViewModel VillageNewsItemList(string NodeAliasPath = "", int villageId = 0, string state = "", int page = 1, int pageSize = 4)
{
try
{
List<VillageNewsItem> newsList = pagesRetriever.RetrieveAsync<VillageNewsItem>(
query => query
.Path("/Home/Village-News", PathTypeEnum.Children)
.Published(true)
.OnSite(SiteContext.CurrentSiteName)
.OrderByDescending(x => x.DocumentCreatedWhen)
)?.Result?.ToList();
var model = new VillageNewsListViewModel
{
VillageNewsItems = newsList, // returns 0 records
};
return model;
}
catch (Exception ex)
{
ErrorHandler.EventLog.LogError(ex.Source, ex.Message, ex.StackTrace);
//return RedirectToAction("ErrorPage", "Error");
}
return null;
}
Couple things I see.
You're calling IPageRetriever.RetrieveAsync, but you aren't putting an await before it. There may be some odd behavior due to this. Get rid of the ?.Result?.ToList() and instead just put await before it, it will return an IEnumerable of the specified type.
You don't need ".Published" nor "OnSite" with IPageRetriever, this API automatically uses the Current Site Context, the current culture, and either Published or not / Latest Version or not based on if it's in edit/preview mode or not.
See if those things fix the issue!
I also asume it is caused by async context here...
You can try to use a document query instead.
Would be something like this:
var items = new DocumentQuery<VillageNewsItem>(
.Path("/Home/Village-News", PathTypeEnum.Children)
.PublishedVersion()
.Published()
.OnCurrentSite()
.OrderByDescending(x => x.DocumentCreatedWhen))
?.Result
?.ToList();
If you have multiple cultures, add the culture to your query, too.
.Culture(LocalizationContext.CurrentCulture.CultureCode)

how to stream user query instead of awaiting it

so, I am using a search Delegate to allow user search, however, when user is inputting data, it's really slow, it lags and skips frames as they type, because my search delegate loops through a list that contains about 2000 objects and checks user query against each object's title and main text, making about 4000 search criteria in total, this was causing the lag, so to solve this, I simply spawned a new isolate to handle the suggestions building, as such
Future<List<Data>> allData(String query) async {
List<Data> suggestions;
List<Data> myData(String message) {
return allData.where((data) {
final result = data.dataText.replaceAll(RegExp('[^A-Za-z0-9 ]'), '').toLowerCase();
final result1 = data.dataText.replaceAll(RegExp('[^A-Za-z0-9]'), '').toLowerCase();
final result2 = data.dataText.toLowerCase();
final result3 = data.dataText.replaceAll("th'incarnate", "the incarnate").toLowerCase();
final input = query.toLowerCase();
final result4 = data.dataTitle.toLowerCase();
final result5 = data.dataTitle.replaceAll(RegExp('[^A-Za-z0-9 ]'), '').toLowerCase();
final result6 =data.dataTitle.replaceAll(RegExp('[^A-Za-z0-9]'), '').toLowerCase();
return result.contains(input) ||
result1.contains(input) ||
result2.contains(input) ||
result3.contains(input) ||
result4.contains(input) ||
result5.contains(input) ||
result6.contains(input);
}).toList();
}
query.isEmpty
? suggestions = []
: suggestions = await compute<String, List<Data>>(
myData,
"",
);
if (query.isNotEmpty && suggestions.isEmpty) {
return [
Data(
DataTitle: "No Results",
DataText: "No Results",
),
];
} else {
return suggestions;
}
}
then in buildSuggestions I did this
#override
Widget buildSuggestions(BuildContext context) {
return FutureBuilder<List<Data>>(
future: DataBrain().allData(query),
builder: (context, snapshot) {
List<Data> suggestions;
query.isEmpty ? suggestions = [] : suggestions = snapshot.data!;
return ListView.builder(
itemCount: suggestions.length,
itemBuilder: (context, suggestIndex) {
final suggestion = suggestions[suggestIndex];
if (snapshot.connectionState == ConnectionState.done) {
if (suggestion.dataText.toLowerCase() == "no results") {
return const ListTile(
title: Text("No results"),
subtitle: Text("Please check for spelling mistakes"),
);
} else {
return ListTile(
title: Text(suggestion.dataTitle),
trailing: Text(
'${suggestion.dataBookName} ${suggestion.dataBookNumber == 0 ? '30a' : suggestion.dataBookNumber}',
style: const TextStyle(
fontStyle: FontStyle.italic,
fontWeight: FontWeight.w300,
),
),
onTap: () {
close(context, suggestion);
},
);
}
} else {
return const ListTile(
title: Text('Loading...'),
);
}
});
});
}
it works fine without lags, however now, the suggestions don't build as user types, it waits till user is no longer typing, for like 2 seconds, then it builds suggestions, I think this is because it's a future and in a future builder, I'm not sure why. I'm thinking of turning it into a stream rather and listen to query change and build immediately instead of awaiting for two seconds after user stops querying, is there any workaround, what's a good way to eliminate the delay, i need it to filter the list and build suggestions with each keystroke, as it used to do before i moved it to a new isolate and made it a future

Loading URL with unique Android or iOS device id in webview

I know how to get the device unique id in flutter, but when I add it as a parameter to url and call it with webview, I can't get the result I want. When I call the following function, webview is loaded with empty device id parameter because it is working ASYNC and not yet completed and not returning any value.
Future<String> _getId() async {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Theme.of(context).platform == TargetPlatform.iOS) {
IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
return iosDeviceInfo.identifierForVendor; // unique ID on iOS
} else {
AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
return androidDeviceInfo.androidId; // unique ID on Android
}
}
The webview loading function is below;
String webViewUrl;
_getId().then((id) {
webViewUrl = "http://websiteblabla.com/?deviceid="+id;
return WebviewScaffold(
url: webViewUrl,
withJavascript: true,
withZoom: false
);
}
How can I get URL with unique Android or iOS device id in webview when it is ready?
You can create Future function or method which will return WebviewScaffold widget when everything is ready. And the use it with FutureBuilder. It would look something like this.
FutureBuilder(
future: _buildWebview(),
builder: (context, snapshot) {
if(!snapshot.hasData) return Center(
child: CircularProgressIndicator(),
);
return snapshot.data;
},
),
Future<Widget> _buildWebview() async {
await _getId().then((id) {
webViewUrl = "http://websiteblabla.com/?deviceid="+id;
return WebviewScaffold(
url: webViewUrl,
withJavascript: true,
withZoom: false
);
}
}

Reuse spinner in sync methods without repeating code

In our code, we repeat the same sequence multiple times: starting a spinner, then execute a spawnSync method and update the spinner depending on result. For example here is one of the method:
cloneSync() {
const spinner = ora({
text: 'Cloning repository',
color: 'cyan',
spinner: 'arrow3'
}).start();
let clone = spawnSync('git', ['clone', repository.url, repository.name]);
if (clone.stderr) {
spinner.fail('Error while cloning repository');
throw new Error(clone.stderr);
} else {
spinner.succeed('Successfully cloned repository');
return clone.stdout;
}
}
Another code example so you can see the logic is almost identical:
parseLatestTagAndTransmitToDocker() {
const spinner = ora({
text: 'Checking latest tag',
color: 'cyan',
spinner: 'arrow3'
}).start();
let tag = spawnSync('git', ['describe', '--abbrev=0']);
if (tag.stderr) {
spinner.fail('Error while fetching latest tag of repository');
throw new Error(tag.stderr);
} else {
spinner.text(`Successfully retrieved latest tag: ${tag.stdout}`);
let docker = spawnSync('docker', ['run', 'myimage:latest', tag.stdout]);
if (docker.stderr) {
spinner.fail('Error while transmitting tag to docker image');
throw new Error(docker.stderr)
} else {
spinner.success('Successfully transmitted tag to docker service');
return docker.stdout;
}
}
}
Is it possible, in node 8+ to wrap this code and make it more reusable. I struggle finding a reusable code without having to trigger spinner and the if/else condition. Doing so with async allow use of try/catch and await/async. But here with sync method, I don't find the proper way to code that kind of behavior.
From the two examples you've provided, I can see a "SpinnerSpawner" function that returns a promise:
function spinnerSpawner(spinnerConfig, cmd, args) {
if (typeof(spinnerConfig) == "string") spinnerConfig = {
text: spinnerConfig,
color: "cyan",
spinner: "arrow3"
}
return new Promise(function(resolve, reject) {
let spinner = ora(spinnerConfig).start,
tag = spawnSync(cmd, args)
if (!tag.stdError) {
resolve(spinner, tag)
} else {
reject(spinner, tag)
}
})
}
cloneSync() {
spinnerSpawner("cloning repository", "git", ["clone", repository.url, repository.name])
.then(function(spinner, proc) {
spinner.succeed('Successfully cloned repository');
return proc.stdout;
}, function(spinner, proc) {
spinner.fail('Error while cloning repository');
throw new Error(proc.stderr);
}
)
}
parseLatestTagAndTransmitToDocker() {
spinnerSpawner("Checking latest tag", "git", ["describe", "--abbrev=0"])
.then(
function(spinner, proc) {
spinner.text(`successfully retrieved latest tag: ${proc.stdout}`)
return spinnerSpawner("checking docker", "docker", ["run", "myimage:latest", proc.stdout])
}
).then(
function(spinner, proc) {
spinner.success("Processing completed")
return proc.stdout
},
function(spinner, proc) {
spinner.fail(`processing error: ${proc.stderr}`);
throw new Error(tag.stderr);
}
)
}
as always, my code is pseudo-code and not fit for execution - let alone production!

Resources